问题
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