VBA to export a specific worksheet to txt file

Johnny_H

New Member
Joined
Jan 23, 2014
Messages
12
Hi all,

I would like to create a macro to export Column A of a particular worksheet to a text file (Text file to have the extension .tcl).
Is it also possible for the exported text file not to have "" around the exported data?

Your help would be much appreciated!
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,604
Try this. Change "Sheet1" and the file name as required.
Code:
Public Sub Save_Column_A()

    Workbooks.Add
    With ActiveWorkbook
        ThisWorkbook.Sheets("Sheet1").Columns("A:A").Copy .Sheets(1).Range("A1")
        .SaveAs "C:\Path\To\File.tcl", FileFormat:=xlText
        .Close False
    End With
    
End Sub
 

Johnny_H

New Member
Joined
Jan 23, 2014
Messages
12
Thanks John_w,

I'm getting a "Run-time error '9': Subscript out of range.

Is it possible to have the sheet exported without opening a new workbook?
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,604
Which line causes the error? I'm guessing ThisWorkbook.Sheets("Sheet1"), which means you don't have a sheet called "Sheet1".

Try this code which saves column A directly without opening a new workbook (again change the "Sheet1" and file path and name in the code to suit your situation).
Code:
Public Sub Save_Column_A2()

    Dim columnA As Variant

    With Worksheets("Sheet1")
        columnA = Application.Transpose(.Range("A1", .Cells(.Rows.Count, "A").End(xlUp)).Value)
    End With
    
    Open "C:\Path\To\File.tcl" For Output As #1
    Print #1, Join(columnA, vbCrLf)
    Close #1
    
End Sub
 

Johnny_H

New Member
Joined
Jan 23, 2014
Messages
12
Thanks John-W unfortunately I still get an error code when I run the macro (even when changing the sheet name and path).

My colleague has been in touch and provided me with this code which seems to work, although I'll admit to not fully understanding how!

Public Sub Save_Column_A2()


Dim columnA As Variant


With Worksheets("Centroid_str_export")
columnA = Application.Transpose(.Range("A1", .Cells(.Rows.Count, "A").End(xlUp)).Value)
End With

Open "C:\Desktop" For Output As #1
Print #1, Join(columnA, vbCrLf)
Close #1

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,224
Messages
5,594,915
Members
413,952
Latest member
JGer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top