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!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,388
Members
448,957
Latest member
Hat4Life

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
Back
Top