How to use Visual Basic to generate .txt file

LanaDallas

New Member
Joined
Feb 15, 2019
Messages
2
Could you help me to create the .txt file from the Excel spreadsheet. What Visual Basic code would I use?

The correct format to use for the text file is:
Document Number + Tab Key + Amount (with a decimal point and no dollar sign) + Enter Key. Repeat for each request I need to process.

Text File Example:
X000X000000[Tab]1000.00[Enter]
X000X000000[Tab]1000.00[Enter]
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
This code will save the workbook as a tab delimited .txt file, change the file path as desired.
Code:
ActiveWorkbook.SaveAs Filename:= _
        "Macintosh HD:Users:michaelerickson:Desktop:Workbook1.txt", FileFormat:=xlText, CreateBackup:=False

or you could use
Code:
Application.Dialogs(xlDialogSaveAs).Show "test", 3
 
Upvote 0
Hello Lana
WELCOME to the site. Here is my suggested way to write your data to a text file, though after seeing the amazing code that Mr. Rickson posted, I have no idea why anyone would use mine. I'm embarassed to even post it here.

Code:
Sub WriteToTextFile()
' This assumes the data starts in cell 'A2'
   Dim NumRows As Long
   Dim RowCounter As Long
NumRows = Range("A" & Rows.Count).End(xlUp).Row
Open "LanaDallas.Txt" For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
    For RowCounter = 2 To NumRows
        Write [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , Cells(RowCounter, 1) & Chr$(9) & Cells(RowCounter, 2) & Chr$(27)
    Next RowCounter
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
End Sub

TotallyConfused
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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