Save a txt file via VBA

gedinfo

New Member
Joined
Aug 24, 2011
Messages
9
Hello,

I wish to be able to save some information from an excel spreadsheet to a txt file.

Two items that I need to resolve:

What is the proper way to copy the spreadsheet data to the file? I already have the code in to open the file.

The code below does not actually copy the data.

What is the best way to save this .txt file with VBA commands?

Thank you.

snippet:
If Dir("C:\temp\Parse.txt") <> "" Then
Open "C:\temp\Parse.txt" For Output As #1
Dim TxtToWrite As String
TxtToWrite = "Maxima "
Print #1, TxtToWrite
End If
 
Last edited:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
What is the proper way to copy the spreadsheet data to the file?

Not to be critical, but your question is very vague.

  • Do you want each cell on its own line?
  • Or do you want each row on it's own line?
  • If writing by row, then separate the cells with; quotes, commas, spaces, tabs ?

Excel can already save a sheet as a comma separated text file (.csv). This could easily be opened in any text or word program and manipulated.

You can also simply copy a range of cells and paste them in Notepad.

There are several Excel add-ins that can do this. Some are free for trial or individual use.

If you search this forum, you can find a lot of example code to variations on this theme.
 
Upvote 0
I have a column(G) that has text in it.
I have a column(B) that has data in it.

I wish to be able to search for certain text in column (G) and then take the data from Column(B) and write this to a file.

Example:
I search for "DataOne" in Column(G)
If this text exists in column(G), then take the value from column(B).

So, this string "DataOne 'value' is written to a .txt file.
There are many instances of this in this spreadsheet(2048 rows).

I wish to be able to search the entire spreadsheet for this text("DataOne") and write to a file.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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