[英]VBA code to save Excel sheet as tab-delimited text file
I found this code and tried it.我找到了这段代码并试了一下。 It works fine, but in the saved text file the first column has blank space (for all rows).
它工作正常,但在保存的文本文件中,第一列有空格(对于所有行)。 I am unable to fix this code.
我无法修复此代码。
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
Hope this might save someone some time:希望这可以节省一些时间:
Sub ExportToTxt()
Dim fileStream As Object
Set fileStream = CreateObject("ADODB.Stream")
fileStream.Charset = "utf-8"
fileStream.Open
' set the range you'd like to export
Dim rangeToExport As Range
Set rangeToExport = Worksheets("BPC-Processed").Range("A1").CurrentRegion
Dim firstCol, lastCol, firstRow, lastRow As Integer
firstCol = rangeToExport.Columns(1).Column
lastCol = firstCol + rangeToExport.Columns.Count - 1
firstRow = rangeToExport.Rows(1).row
lastRow = firstRow + rangeToExport.Rows.Count - 1
' iterate the range, write text to stream
Dim r, c As Integer
Dim str, delimiter As String
For r = firstRow To lastRow
str = ""
For c = firstCol To lastCol
If c = 1 Then
delimiter = ""
Else
delimiter = vbTab ' tab
End If
str = str & delimiter & rangeToExport.Cells(r, c).Value
Next c
fileStream.WriteText str & vbCrLf ' vbCrLf: linebreak
Next r
' flush stream
Dim filePath As String
filePath = Application.ThisWorkbook.Path & "\BPC-Processed.txt"
fileStream.SaveToFile filePath, 2 ' 2: Create Or Update
fileStream.Close
End Sub
Also this works...这也有效...
Sub ExportRange()
Dim ExpRng As Range
Dim myTab As String
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
If c = 1 Then myTab = "" Else myTab = vbTab
' data = ExpRng.Cells(r, c).Value
Data = Data & myTab & ExpRng.Cells(r, c).Value
Next c
Print #1, Data
Next r
Close #1
End Sub
You're prefixing with a vbTab
for each cell, including the first.您为每个单元格添加了一个
vbTab
前缀,包括第一个单元格。 Change the following:更改以下内容:
Data = Data & vbTab & ExpRng.Cells(r, c).Value
to到
If c = FirstCol Then
Data = Data & ExpRng.Cells(r, c).Value
Else
Data = Data & vbTab & ExpRng.Cells(r, c).Value
End If
Alternatively, if there will always be some data on each line, you could just strip the first vbTab
from each line during the Print
stage by:或者,如果每行总是有一些数据,您可以在
Print
阶段通过以下方式从每行中删除第一个vbTab
:
Changing改变
Print #1, Data
to到
Print #1, Mid(Data, 2, Len(Data)-1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.