VBA to append a text file Excel

Home/VBA/VBA to append a text file Excel

VBA code to VBA to append a text file 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 using VBA to append a text file using Excel VBA.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


Appending a text file Using VBA

Here is the Procedure, Example VBA Syntax and Example VBA Macro code for appending a text file in Excel VBA. This will help you to know how to append a text file and add data using VBA.

VBA to append a text file: Procedure

We will first open the text file as Append file with a file number. Then we will add the data to the file using File Number.

VBA to append a text file: Syntax

Here is the VBA code and syntax for Appending a text file Using VBA. Use the Append keyword to open text file in appending mode.

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

VBA to Append a text file: Example Macro Code

Following is the sample Excel Macro to write to a text file by appending it using Excel VBA.

Sub VBA_to_append_text_file()
Dim strFile_Path As String
strFile_Path = "C:\temp\test.txt" ‘Change as per your test folder and exiting file path to append it.
Open strFile_Path For Append As #1
Write #1, "This is my sample text"
Close #1
End Sub 

Instructions to run the VBA Macro code to append a text file

Please follow the below steps to execute the VBA code to Append a text file using Excel VBA Editor.
1. 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)]
2. Step 2: Press Alt+F11 to open the VBA Editor [You can also open the VBE from the Developer Tab in the Excel ribbon]
3. 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]
4. Step 4: Copy the above Example Macro code and paste in the code module which have inserted in the above step
5. Step 5: Change the folder path as per your testing folder structure and existing file path.
6. Step 6: Now press the F5 to Run and Execute the Macro.
7. 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 appended with the data which you have mentioned in the code.

VBA to Append a text file – Macro Explained

Here is the detailed explanation of the Excel macro to append a text file using VBA.

  • Starting the program and sub procedure to write VBA code to append a text file and adding the data.
  • 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 Append 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 append a text file and updating the data.

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

‘Starting the program and sub procedure to write VBA code to write the data to a text file Append.
Sub VBA_to_append_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:\temp\test.txt”

‘Opening the text file for Append with FileNumber as 1.
Open strFile_Path For Append 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 append the data in text file.

LIMITED TIME OFFER
By |January 19th, 2015|VBA|0 Comments

About the Author:

PNRao is a passionate business analyst and having close to 10 years of experience in Data Mining, Data Analysis and Application Development. This blog is his passion to learn new skills and share his knowledge to make you expertise in Data Analysis (Excel, VBA, SQL, SAS, Statistical Methods, Market Research Methodologies and Data Analysis Techniques).

Leave A Comment


Related pages


sql dml commands with examplesexcel vba cell addressvba print statementopen excel file using vbavba filter excelactivate macros in excel 2007scheduling template excelvba code to export access query to excelusing functions in vbaunlock protected excel filevba excel option buttonexcel macro to merge cellshow to put numbers in ascending order in excelhow to merge cells in microsoft excelexcel sort macrohow to make a 100 stacked bar chart in excelexcel vba copy rangehow to create activex controlexcel chart objectcompound if statement in excelproject tracker template excelexcel vba codeswhat is the use of vlookup and hlookup in excelwhat is ddl and dml in sql serverisblank excel functionvlookup between two valuesworkbooks.saveassimple macro in exceladvanced vlookup excelexcel task management template freesort worksheets alphabeticallyexcel formula in vba codeexcel vba combo boxvba form excelword vba exampleschar in excelformula for vlookup in excel 2007vba usesdata analytics interview questions and answersexcel formula codesuserform in exceldatevalue vbahow to set color in excel by formulaexcel if statement syntaxexcel vba addressexcel isequalfind last row vbaremove blank rows in excelvba excel textboxsumif formula examplecheck duplicate in excelhow to turn on developer tab in excel 2010using vlookup in excel 2010protected workbook forgot passwordexample of excel formulasvba entire row deletewhat is a logical function in excelclear cells vbashow developer tab excel 2007advance vbaexample for vlookupexcel vba delete rowsvba public variabledata definition language commandssql query on excelrecordset sqlvba listbox rowsourceadvanced excel formulas with examples in excel sheethow do i merge columns in excelpivot table templateshow to open xlsm file in excel 2010what is dml in sqlvba code for msgboxhow to unhide row in excellistbox vbahow to insert column in excelestimating templates