VBA Save worksheet as text

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Good Day All,

I managed to come up with a vba code that I am comfortable in using with respect to converting a specified worksheet into a txt file.

However, I would like to know where I can specify which sheet range?


VBA Code:
Sub ExportToTXT()

Dim columnA As Variant

With Worksheets(9)
columnA = Application.Transpose(.Range("A1", .Cells(.Rows.Count, "A").End(xlUp)).Value)
End With

ThisWorkbook.SaveAs ThisWorkbook.path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt", 20

End Sub


For example, if I want to capture only for the range between A1:AY36.

Where would I put this into the code?

Kindly let me know.

Thank you!
pinaceous
 
Untitled1 .jpg


Should be good to go.

Let me try the code on a fresh workbook.

Thanks,
Pinaceous
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hey Logit!

I really do appreciate your code!

But I am getting an error message on line:

VBA Code:
Open Myfile1 For Output As #1

Do I have to set something up in my library?

Please let me know.

Thank you,
pinaceous

Hey Logit,

I've opened a fresh workbook inserted this code into a module after I confirmed the library and the same error pops at:

VBA Code:
Open Myfile1 For Output As #1

Kindly let me know.

Thank you,
pinaceous
 
Upvote 0
What is the error code you are receiving ?
 
Upvote 0
Unusual, works fine here.

Regarding this line of code :

VBA Code:
'Assigning a file name with timestamp

   Myfile1 = ThisWorkbook.Path & "\" & "EmployeeData-" & Format(Now, "mmdd-hhmmss") & ".txt"

Change it to :

Code:
'Assigning a file name with timestamp

   Myfile1 = ThisWorkbook.Path & "\" & "EmployeeData " & Format(Now, "mmdd hhmmss") & ".txt"

(You'll be removing the dashes only)

I learned someone else was having same problem because of "special characters" in the file name.
 
Upvote 0
One other thing you can try ... open NOTEPAD and save a blank file with the same name outlined in the macro code.
Place that blank NOTEPAD file in the same location as the workbook and run the macro again. It might be because on your machine
the macro is expecting the file to already exist. So instead of creating the file as it does on my machine, it errors out on yours.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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