我用下面的代碼行導出到單獨的文本文件:VBA:退出For循環當行空
Sub export_Test()
Dim firstRow As Integer, lastRow As Integer, fileName As String
Dim myRow As Integer, myStr As String
firstRow = 10
lastRow = 29
For myRow = firstRow To lastRow
fileName = "C:\mallet\test\" & Cells(myRow, 1) & ".txt"
Open fileName For Append As #1
myStr = Cells(myRow, 2).Value
Print #1, myStr
Close #1
Next
End Sub
的問題是,這個代碼是特定的行數。我想用這個代碼來處理不同的數據樣本,因此excel文件中的行數會有所不同,並且可能會有數千個數。我需要將lastRow變量設置爲無限數字,並在For循環遇到空行時退出。
http://stackoverflow.com/questions/11169445/error-finding-last-used-cell- in-vba – 2013-02-20 21:10:51
此外,由於您使用的是行,建議將它們聲明爲「Long」而不是「整數」:) – 2013-02-20 21:13:01
@Sid在32位操作系統上,您應該_always_使用'Long',除非特別需要使用16位數字('Integer') – 2013-02-21 06:09:14