VBA Save Worksheet # as a *.txt

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Good Day Everyone!

I'd like to create a sub where it can save the entire worksheet contents as a *.txt file.

I do have a sub now that seems to work but it is a work in progress.

Does anyone have any codes?

Many thanks,
Pinaceous


VBA Code:
Sub saveText()

    ActiveWorkbook.SaveAs Filename:= _
        ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt", FileFormat:=xlText, _
        CreateBackup:=False
      
    
End Sub

PS. This code does not work because it changes the worksheet name to
VBA Code:
textfile-" & Format(Now, "ddmmyy-hhmmss")
and I cannot seem to prevent this!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
The following writes highlighted Excel data to Notepad :

Sub Write2Text() Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer myFile = Application.DefaultFilePath & "\Sales.txt" Set rng = Selection Open myFile For Output As #1 For i = 1 To rng.Rows.Count For j = 1 To rng.Columns.Count cellValue = rng.Cells(i, j).Value If j = rng.Columns.Count Then Write #1, cellValue Else Write #1, cellValue, End If Next j Next i Close #1 End Sub
 
Upvote 0
Or try this line

VBA Code:
ThisWorkbook.SaveAs ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt", 20
 
Upvote 0
Or try this line

VBA Code:
ThisWorkbook.SaveAs ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt", 20
Hey JEC,

In experimenting with your code, I really appreciate that the code was able to capture all of the Worksheets data.

However, I do notice that the sub changes the Worksheets name to
VBA Code:
textfile-" & Format(Now, "ddmmyy-hhmmss"
and it also changes the Workbooks name.

Do you know how to prevent your sub from changing both the Worksheet and the Workbook name to
VBA Code:
textfile-" & Format(Now, "ddmmyy-hhmmss"
??

Please let me know and thanks again for the code!

R/
pinaceous
 
Upvote 0
Or try this line

VBA Code:
ThisWorkbook.SaveAs ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt", 20
Hi JEC,

Is there a way to specify a range that the text file captures?

For example, if I want range A1:AY38.

Please let me know.

And thank you!
pinaceous
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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