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
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
See if this works. Change the range to suit.

VBA Code:
Sub ExportToTXT()

With Worksheets(9)
    Range("A1:E54")
End With

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

End Sub
 
Upvote 0
Good Day Logit!

Thanks for your post but I am getting an error message with respect to:

VBA Code:
 Range("A1:E54")

Any suggestions?

Thanks!
pinaceous
 
Upvote 0
Untitled .jpg
 
Upvote 0
Here is a different approach. Edit the range as required.

[CODE=vba]Option Explicit Sub Saveastext1() 'Declaring the variables Dim Myfile1 As String Dim Line1 As String Dim MyRange1 As Range, x, y Dim i As Long 'Assigning a file name with timestamp Myfile1 = "EmployeeData-" & Format(Now, "mmdd-hhmmss") & ".txt" 'Open the file to write data on it. Open Myfile1 For Output As #1 'Setting the range of data to be exported. Set MyRange1 = Range("A1:E5") 'Looping through each cell to save data and apply delimeter as comma For x = 1 To MyRange1.Rows.Count For y = 1 To MyRange1.Columns.Count Line1 = IIf(y = 1, "", Line1 & ",") & MyRange1.Cells(x, y) Next y Print #1, Line1 Next x 'close the file Close #1 End Sub[/CODE]
 
Upvote 0
Here is a different approach. Edit the range as required.

[CODE=vba]Option Explicit Sub Saveastext1() 'Declaring the variables Dim Myfile1 As String Dim Line1 As String Dim MyRange1 As Range, x, y Dim i As Long 'Assigning a file name with timestamp Myfile1 = "EmployeeData-" & Format(Now, "mmdd-hhmmss") & ".txt" 'Open the file to write data on it. Open Myfile1 For Output As #1 'Setting the range of data to be exported. Set MyRange1 = Range("A1:E5") 'Looping through each cell to save data and apply delimeter as comma For x = 1 To MyRange1.Rows.Count For y = 1 To MyRange1.Columns.Count Line1 = IIf(y = 1, "", Line1 & ",") & MyRange1.Cells(x, y) Next y Print #1, Line1 Next x 'close the file Close #1 End Sub[/CODE]
Hey Logit,

I do appreciate the code but it does not function for me?

Does it work for you?

Thanks,
pinaceous
 
Upvote 0
The previous version is saving your TEXT file to the DOCUMENTS folder.

This version will save the TEXT file to the same location as the current workbook :

VBA Code:
Option Explicit

Sub Saveastext1()

'Declaring the variables

   Dim Myfile1 As String

   Dim Line1 As String

   Dim MyRange1 As Range, x, y
  
   Dim i As Long

'Assigning a file name with timestamp

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

'Open the file to write data on it.

   Open Myfile1 For Output As #1

'Setting the range of data to be exported.

   Set MyRange1 = Range("A1:E5")

'Looping through each cell to save data and apply delimeter as comma

   For x = 1 To MyRange1.Rows.Count

       For y = 1 To MyRange1.Columns.Count

           Line1 = IIf(y = 1, "", Line1 & ",") & MyRange1.Cells(x, y)

       Next y

       Print #1, Line1

   Next x

'close the file

   Close #1

End Sub
 
Upvote 0
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
 
Upvote 0
Be certain to include the following references in your workbook :

ref.jpg
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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