我找到了这段代码并尝试运行它,它可以正常工作,但是保存的文本文件中第一列有空格(所有行都有)。我无法修复这个问题。
Sub ExportRange()
Dim ExpRng As Range
Open ThisWorkbook.Path & "\AllDXL.txt" For Output As #1
Set ExpRng = Worksheets("Sheet1").Range("A1").CurrentRegion
FirstCol = ExpRng.Columns(1).Column
LastCol = FirstCol + ExpRng.Columns.Count - 1
FirstRow = ExpRng.Rows(1).Row
LastRow = FirstRow + ExpRng.Rows.Count - 1
For r = FirstRow To LastRow
Data = ""
For c = FirstCol To LastCol
' data = ExpRng.Cells(r, c).Value
Data = Data & vbTab & ExpRng.Cells(r, c).Value
Next c
Print #1, Data
Next r
Close #1
End Sub
IF ELSE THEN
结构),那么你就不需要更改Print
。如果你同时使用两个但将Print
部分更改为Print#1,Mid(Data,1,Len(Data)-1)
,则会丢失每行的最后一个字符。 - CLR