VBA: Export range to text file

maraki

New Member
Joined
May 2, 2014
Messages
15
Hi all,

~Great forum - VBA newbie here!

I am looking for a macro which exports to a text file, the range A1:E* where * is the last used cell in column E.

Columns should be separated by tabs in the text file.

any ideas?

many thanks
Maria x
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Code:
Sub CrText()


Dim c00            As Variant
Const textFilePath As String = "C:\Desktop\myTextFile.txt"
Dim lngCounter     As Long
Dim FF             As Integer


FF = VBA.FreeFile
c00 = Range("A1").CurrentRegion


Open textFilePath For Output As #FF
    For lngCounter = LBound(c00, 1) To UBound(c00, 1)
        Print #FF, Join(Application.Index(c00, lngCounter, 0), vbTab)
    Next
Close #FF
MsgBox "Your text file has been created."
End Sub
 
Upvote 0
Welcome to MrExcel.

Turn on the macro recorder. Select A1 and choose Go To Special Current Region. Copy the selected range. Insert a new workbook and Paste. Save the active workbook as a txt file. Turn off the macro recorder and look at the recorded code. Adjust it to suit.
 
Upvote 0
Code:
Sub CrText()


Dim c00            As Variant
Const textFilePath As String = "C:\Desktop\myTextFile.txt"
Dim lngCounter     As Long
Dim FF             As Integer


FF = VBA.FreeFile
c00 = Range("A1").CurrentRegion


Open textFilePath For Output As #FF
    For lngCounter = LBound(c00, 1) To UBound(c00, 1)
        Print #FF, Join(Application.Index(c00, lngCounter, 0), vbTab)
    Next
Close #FF
MsgBox "Your text file has been created."
End Sub


thanks this is very useful. However, the macro will be used by a number of users and they don't necessarily have a C: drive which could lead to errors. I don't need the text file to be saved anywhere - just appear in front of users. Could we amend this detail please?

@andrew: thanks i am already learning how to use the recorder !

x
 
Upvote 0
a text file needs to be create before you can write on it. You can let VBA choose where to create it depending on who is using your macro.

Code:
textFilePath = CStr(VBA.CurDir) & "\mTextFile.txt"

try

Code:
Sub CrText()


Dim c00            As Variant
Dim textFilePath   As String
Dim lngCounter     As Long
Dim FF             As Integer


textFilePath = CStr(VBA.CurDir) & "\mTextFile.txt"


FF = VBA.FreeFile
c00 = Range("A1").CurrentRegion


Open textFilePath For Output As #FF
    For lngCounter = LBound(c00, 1) To UBound(c00, 1)
        Print #FF, Join(Application.Index(c00, lngCounter, 0), vbTab)
    Next
Close #FF
Workbooks.OpenText textFilePath
End Sub


thanks this is very useful. However, the macro will be used by a number of users and they don't necessarily have a C: drive which could lead to errors. I don't need the text file to be saved anywhere - just appear in front of users. Could we amend this detail please?

@andrew: thanks i am already learning how to use the recorder !

x
 
Last edited:
Upvote 0
PS

if you wanna open it in a notepad then replace

Workbooks.OpenText textFilePath

with

Shell "C:\WINDOWS\notepad.exe " & textFilePath, vbNormalFocus
 
Upvote 0
ok I have tested this and it works but instead of opening this in a workbook, could the file just open in notepad or the default text editor?!

edit: you answered my question first! But what if the user is using MAC ?
 
Upvote 0
I have never used VBA on a Mac. But I am assuming it has a default folder where the notepad.exe is located. You need to know where that folder is and then you can update the code accordingly :)
 
Upvote 0

Forum statistics

Threads
1,223,574
Messages
6,173,146
Members
452,502
Latest member
PQCurious

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