VBA code to write to a text file from Excel Range Macros Examples for writing to text files using VBA in MS Office Word, PowerPoint, Access, Excel 2003, 2007, 2010, 2013 and VBScript. This Example VBA Program and function will help you to know how to read the data from excel and write to text file from a Range using Excel VBA.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

BROWSE ALL TEMPLATES

50+ Project Management Templates Pack
Excel PowerPoint Word

VIEW DETAILS

Advanced Project Plan & Portfolio Template
Excel Template

VIEW DETAILS

Ultimate Project Management Template
Excel Template

VIEW DETAILS

20+ Excel Project Management Pack
Excel Templates

VIEW DETAILS

20+ PowerPoint Project Management Pack
PowerPoint Templates

VIEW DETAILS

10+ MS Word Project Management Pack
Word Templates

VIEW DETAILS


Writing to a text file from Excel Range Using VBA

Here is the Procedure, Example VBA Syntax and Example VBA Macro code for writing to a text file from Excel Range. This will help you to know how to write to a text file using VBA.

VBA write to a text file from Excel Range: Procedure

We will first open the text file for writing as output file with a file number. Then we will read the data from excel and write to the text file using File Number.

VBA write to a text file from Excel Range: Syntax

Here is the VBA code and syntax for Writing to a text file Using VBA. Here we are reading the data from Range A1 and Writing to the text File.

strFile_Path = "Your file Path"
Open strFile_Path For Output As #1
Write #1, Range(“A1”)

VBA write to a text file from Excel Range: Example Macro Code

Following is the sample Excel Macro to write to a text file from worksheet using Excel VBA. We are reading the first to rows in column A and writing to the text file using For loop.

Sub VBA_write_to_a_text_file_from_Excel_Range()
    Dim iCntr as Lonng
    Dim strFile_Path As String
    strFile_Path = "C:\temp\test.txt"

    Open strFile_Path For Output As #1
For iCntr = 1 To 10
    Write #1, Range("A" & iCntr)
Next iCntr
    Close #1
End Sub 

Instructions to run the VBA Macro code to read from excel and write to a text file

Please follow the below steps to execute the VBA code to read the data from Excel and write to a text file using Excel VBA Editor.
Step 1: Open any Excel workbook [ To Open MS Excel: Go to Start menu, All programs and select Excel from MS Office OR You can simply type excel in the run command (Press Windows+ r key to open run dialog)]
Step 2: Press Alt+F11 to open the VBA Editor [You can also open the VBE from the Developer Tab in the Excel ribbon]
Step 3: Insert a code module [Go to insert menu in the VBE and then press Module OR Simply press the Alt+i then m to insert code module]
Step 4: Copy the above Example Macro code and paste in the code module which have inserted in the above step
Step 5: Change the folder path as per your testing folder structure
Step 6: Enter some sample data in Range A1 to A10 for testing purpose
Step 7: Now press the F5 to Run and Execute the Macro
You can press the F8 to debug the macro line by line and see the result immediately

Once you are done with the macro execution, now you can observe that a text file in the test folder. And the text file in the folder is updated with the data from the specified range A1 to A10.

VBA write to a text file from Excel Cells Macro Explained

Here is the detailed explanation of the Excel macro to write to text file from Excel Cells using VBA.

  • Starting the program and sub procedure to write VBA code to read data from Excel and write the data to a text file.
  • Declaring iCntr variable as Long Data Type to use it in for loop to repeat the range A1 to A10.
  • Declaring the strFile_Path variable as String Data Type to store the text file path.
  • Assigning the File path to the variable strFile_Path.
  • Opening the text file for Output with FileNumber as 1.
  • ‘Looping through the cells in the range A1 to A10 and writing the date from excel into the File using FileNumber and Write Command.
  • Closing the File using FileNumber.
  • Ending the Sub procedure to write VBA Code to read data from Excel and write the data to a text file.

Here is the commented macro code for writing to text file using VBA.

‘Starting the program and sub procedure to write VBA code to read data from Excel and write the data to a text file.
Sub VBA_write_to_a_text_file_from_Excel_Range()

‘Declaring iCntr variable as Long Data Type to use it in for loop to repeat the range A1 to A10.
Dim icntr As Long

‘Declaring the strFile_Path variable as String Data Type to store the text file path.
Dim strFile_Path As String

‘Assigning the File path to the variable strFile_Path.
strFile_Path = “C:\temp\test.txt”

‘Opening the text file for Output with FileNumber as 1.
Open strFile_Path For Output As #1

‘Looping through the cells in the range A1 to A10 and writing the date from excel into the File using FileNumber and Write Command.
For icntr = 1 To 10
Write #1, Range(“A” & icntr)
Next icntr

‘Closing the File using FileNumber.
Close #1
Ending the Sub procedure to write VBA Code to read data from Excel and write the data to a text file.

VBA write to a text file from Excel Range without double quotes: Example Macro Code

Following is the sample Excel Macro to write to a text file without double quotes from worksheet using Excel VBA. We are reading the rows first in column A and writing to the text file using For loop.

Sub VBA_write_to_a_text_file_from_Excel_Range()
    Dim iCntr
    Dim strFile_Path As String

    strFile_Path = "C:\temp\test.txt"

    Open strFile_Path For Output As #1

    For iCntr = 1 To 10
        Print #1, Range("A" & iCntr)
    Next iCntr

    Close #1
End Sub 
LIMITED TIME OFFER - Get it Now!
Advanced Project Plan Excel Template

 
Related Resource External VBA Reference