Export Excel range to TXT stop at empty cell

佐手、 提交于 2019-12-11 17:24:34

问题


Found following script

Sub saveText2()
Dim filename As String, lineText As String
Dim myrng As Range, i, j

filename = ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt"

Open filename For Output As #1

Set myrng = Range("Name")

For i = 1 To myrng.Rows.Count
    For j = 1 To myrng.Columns.Count
        lineText = IIf(j = 1, "", lineText & ",") & myrng.Cells(i, j)
    Next j
    Print #1, lineText
Next i
Close #1
End Sub

But I don´t seems to manage to stop the loop at first empty row.

I´ve tried to add a DO While myrng <> "" Loop but I don´t know where to apply the code.


回答1:


Not sure it i the smartest way but I finally managedby doing this:

Sub saveText2()
Dim filename As String, lineText As String
Dim myrng As Range, i, j

filename = ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt"

Open filename For Output As #1

Set myrng = Range("A:B")

For i = 1 To myrng.Rows.Count
    For j = 1 To myrng.Columns.Count

    If IsEmpty(myrng.Cells(i, j)) = True Then
    Close #1
    Exit Sub
    End If

        lineText = IIf(j = 1, "", lineText & " ") & myrng.Cells(i, j)
    Next j
    Print #1, lineText
Next i
Close #1
End Sub


来源:https://stackoverflow.com/questions/46979199/export-excel-range-to-txt-stop-at-empty-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!