我有一个需要导出为.txt文件的表(Table8)。 我当前使用的代码可以正常工作,但是还可以从表中导出空行,从而使.txt文件混乱,以备将来使用。
该表将在1到n行(通常不超过100行)中包含信息。 我只想导出其中包含文本的行(其他行中包含公式,并且当前在.txt文件中显示为空白)。
我当前正在使用的代码:
Sub saveToMtext()
Dim filename As String, lineText As String
Dim myrng As Range, i, j
filename = ThisWorkbook.path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt"
ChDir (ThisWorkbook.path)
Open "m_text.txt" For Output As #1
FileFormat = xlText & CreateBackup = False
Set myrng = Range("Table8")
For i = 1 To myrng.Rows.Count
For j = 1 To myrng.Columns.Count
lineText = IIf(j = 1, "", lineText & vbTab) & myrng.Cells(i, j)
Next j
Print #1, lineText
Next i
Close #1
End Sub
答案 0 :(得分:1)
我从常规IF更改为IF x,然后退出并开始工作。
最终代码:
Sub saveToMtext()
Dim filename As String, lineText As String
Dim myrng As Range, i, j
Dim LValue As String
filename = ThisWorkbook.path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt"
ChDir (ThisWorkbook.path)
Open "m_text.txt" For Output As #1
FileFormat = xlText & CreateBackup = False
Set myrng = Range("Table8")
k = 11
For i = 1 To myrng.Rows.Count
LValue = Cells(k, 121)
m = Len(LValue)
If m < 1 Then Exit For
For j = 1 To myrng.Columns.Count
lineText = IIf(j = 1, "", lineText & vbTab) & myrng.Cells(i, j)
Next j
Print #1, lineText
k = k + 1
Next i
Close #1
End Sub