Macro to export a column to .txt file

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Below is a clipboard usage example:
Rich (BB code):
Sub ColumnToTxt1()
 
  Dim s As String, FileName As String, FileNum As Integer
 
  ' Define full pathname to TXT file
  FileName = ThisWorkbook.Path & "\test.txt"
 
  ' Copy range of B column to the clipboard
  Range("B1", Cells(Rows.Count, "B").End(xlUp)).Copy
 
  ' Copy clipboard text to the 's' variable
  With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
     .GetFromClipboard
     s = .GetText
  End With
  Application.CutCopyMode = False
 
  ' Write s to the TXT file
  FileNum = FreeFile
  If Len(Dir(FileName)) > 0 Then Kill FileName
  Open FileName For Binary Access Write As FileNum
  Put FileNum, , s
  Close FileNum
 
End Sub
 
Last edited:
Upvote 0
Thank you ZVI. I am getting a run time error on the below line of the code

With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
 
Upvote 0
That late binding code can be Excel's version or object registration dependent.
Well, try early binding version.
1. Insert to workbook a Userform: VBE - Insert - UserForm
The reference to Microsoft Form 2.0 Object library will appear in VBE - Tools - References.
2. Delete (or not) that form - Right Click on UserForm1 - Remove UserForm1 - No
3. Use the below code
Rich (BB code):
Sub ColumnToTxt2()
 
  Dim s As String, FileName As String, FileNum As Integer
 
  ' Define full pathname of TXT file
  FileName = ThisWorkbook.Path & "\test.txt"
 
  ' Copy range to the clipboard
  Range("B1", Cells(Rows.Count, "B").End(xlUp)).Copy
 
  ' Copy column content to the 's' variable via clipboard
  With New DataObject
     .GetFromClipboard
     s = .GetText
  End With
  Application.CutCopyMode = False
 
  ' Write s to TXT file
  FileNum = FreeFile
  If Len(Dir(FileName)) > 0 Then Kill FileName
  Open FileName For Binary Access Write As FileNum
  Put FileNum, , s
  Close FileNum
 
End Sub
 
Last edited:
Upvote 0
This is beautiful. it worked. I dont have knowledge of VBA so i dont understand what the below is. Is it possible to have a custom button which completes the processing without any prompt

1. Insert to workbook a Userform: VBE - Insert - UserForm
The reference to Microsoft Form 2.0 Object library will appear in VBE - Tools - References.
2. Delete (or not) that form - Right Click on UserForm1 - Remove UserForm1 - No

Thank you so much
 
Upvote 0
You don't need do the points 1 & 2 once more.
Just do it once and save workbook.
After that the code of that workbook will be good for any PC.
 
Last edited:
Upvote 0
Glad it helped, Vikramjeet! :)
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,145
Members
452,615
Latest member
bogeys2birdies

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