REAL-TIME

VBA Projects

Full Access with Source Code
  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

Effortlessly
Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Share Post

VBA write to a text file with quotes Excel 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 write a string to text file with quotes using Excel VBA.

Writing to a text file Using VBA

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

VBA write to a text file with quotes: Procedure

We will first open the text file for writing as output file with a file number. Then we will write to the file using Write command and File Number.

VBA write to a text file with quotes: Syntax

Here is the VBA code and syntax for writing to a text file Using VBA. Use write command instead of print command for writing string with quotes.

strFile_Path = "Your file Path"
Open strFile_Path For Output As #1
Write  #1, “You can write your required text here”

VBA write to a text file with quotes: Example Macro Code

Following is the sample Excel Macro to write to a text file with quotes using Excel VBA.

Sub VBA_Print_to_a_text_file()
    Dim strFile_Path As String
    strFile_Path = "C:temptest.txt" ‘Change as per your test folder path
    Open strFile_Path For Output As #1
    Write #1, "This is my sample text"
    Close #1
End Sub 

Instructions to run the VBA Macro code to write to a text file with quotes

Please follow the below steps to execute the VBA code to write to a text file with quotes 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: 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 which you have mentioned.

VBA write to a text file with quotes – Macro Explained

Here is the detailed explanation of the Excel macro to write to a text file with quotes using VBA.

  • Starting the program and sub procedure to write VBA code to write to a text file with quotes.
  • 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.
  • Writing to the sample text to the File using FileNumber and Write Command.
  • Closing the File using FileNumber.
  • Ending the Sub procedure to write VBA Code to write to a text file with quotes.

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

‘Starting the program and sub procedure to write VBA code to write the data to a text file with quotes.
Sub VBA_write_to_a_text_file()

‘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:temptest.txt”

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

‘Writing to the sample text to the File using FileNumber and Write Command.
Write #1, “This is my sample text”

‘Closing the File using FileNumber.
Close #1
End Sub
‘Ending the Sub procedure to write VBA Code to write to text file with quotes.

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBATags: Last Updated: January 19, 2015

Leave A Comment