Siraj
Siraj

Reputation: 195

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

Upvotes: 3

Views: 8447

Answers (3)

Rohim Chou
Rohim Chou

Reputation: 1269

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

Upvotes: 4

MiguelH
MiguelH

Reputation: 1425

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

Upvotes: 2

CLR
CLR

Reputation: 12279

You're prefixing with a vbTab for each cell, including the first. 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:

Changing

Print #1, Data

to

Print #1, Mid(Data, 2, Len(Data)-1)

Upvotes: 1

Related Questions