vba excel 导出 txt 在excel中某列有1000行,其中有十几个空行,要求每隔3个空行导出一个txt 文件
2个回答
展开全部
Sub main()
Dim n As Integer
Dim i As Integer
Dim St As Integer, En As Integer
Dim Mark As Integer
n = [a65536].End(xlUp).Row
Mark = 0
St = 1
For i = 1 To n
If Cells(i, "a") = "" Then
If Cells(i + 1, "a") = "" And Cells(i + 2, "a") = "" Then
If Mark > 0 Then St = En + 4
En = i - 1
Mark = Mark + 1
Call SaveTxt(St, En, Mark)
End If
End If
Next
St = En + 4
En = n
Call SaveTxt(St, En, Mark + 1)
End Sub
Sub SaveTxt(ByVal S As Integer, ByVal E As Integer, ByVal M As Integer)
Dim Path As String
Dim i As Integer
Dim Str As String
Path = ThisWorkbook.Path & "\" & Left(ThisWorkbook.Name, _
Application.Find(".", ThisWorkbook.Name) - 1) & "_" & M & ".txt"
Open Path For Output As #1
For i = S To E
Str = Cells(i, "a")
Print #1, Str
Next
Close #1
End Sub
更多追问追答
追问
代码很好,但是没有完全解决问题。不是连续出现3个空行就转存,而是累计出现3个空行就转存一次
追答
由于字数限制,我只发出来主程序的修改后的代码,子程序没有改动,你copy到后面好了。
Sub main()
Dim n As Integer
Dim i As Integer
Dim St As Integer, En As Integer
Dim Mark As Integer
n = [a65536].End(xlUp).Row
Mark = 0
St = 1
i = 1
Do While i <= n
If Cells(i, "a") = "" Then
If Cells(i, "a").End(xlDown).Row - i > 2 Then
If Mark > 0 Then St = Cells(En, "a").End(xlDown).Row
En = i - 1
Mark = Mark + 1
Call SaveTxt(St, En, Mark)
i = Cells(i, "a").End(xlDown).Row
End If
End If
i = i + 1
Loop
St = Cells(En, "a").End(xlDown).Row
En = n
Call SaveTxt(St, En, Mark + 1)
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |