VBA write to text file without carriage return Excel

Home/VBA/VBA write to text file without carriage return Excel

VBA write to text file without carriage return Excel Macros Examples for writing to text files using VBA in MS Office Word, PowerPoint, Access, Excel 2003, 2007, 2010, 2013 and VBScript.

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


This Example VBA Program and function will help you to know how to write a string to text file using Excel VBA.

Writing to a text file without carriage return 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 using VBA.

VBA write to text file without carriage return: 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 text file without carriage return: Syntax

Here is the VBA code and syntax for writing to a text file without carriage return Using VBA. You need to end the write or print statement with semicolon (;).

strFile_Path = "Your file Path"
Open strFile_Path For Output As #1
Print #1, "Line1" ;
Print #1, "Line2" ;

VBA write to text file without carriage return: Example Macro Code

Following is the sample Excel Macro to write to a text file without carriage return using Excel VBA.

Sub VBA_write_to_a_text_file_without_carriage_return ()
Dim strFile_Path As String
strFile_Path = "C:\temp\test.txt" ‘Change as per your test folder path
Open strFile_Path For Output As #1
Print #1, "This is my sample text Line1" ;
Print #1, "This is my sample text Line2" ;
Close #1
End Sub 

Instructions to run the VBA Macro code to write to a text file without carriage return

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

VBA write to text file without carriage return – Macro Explained

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

  • Starting the program and sub procedure to write VBA code to write to a text file without carriage return.
  • 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 Print Command. And ending with semicolon (;)
  • Closing the File using FileNumber.
  • Ending the Sub procedure to write VBA Code to write to a text file without carriage return.

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.
Sub VBA_write_to_a_text_file_without_carriage_return ()

‘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

‘Writing to the sample text to the File using FileNumber and Print Command.
Print #1, “This is my sample text Line1″ ;
Print #1, “This is my sample text Line2”;

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

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


vba check boxexcel dynamic cell referencehow to write vlookup formula in excelvba access create tablecombine multiple sheets into one sheet excelvba arrayhow to insert row on excelunlock file excelexcel macro hyperlinkhyperlink examplesvba excel userformexcel 2007 all formulas with examplesexcel vba new worksheetunhide hidden workbookcount columns in excelsimple excel spreadsheet examplesvba commandsaccess vba for loopms excel programming tutorialexcel formula for cell colordelete row excel shortcutdefine vlookup in excelexcel vba torrentunprotecting excelhow to create a new worksheet in excelvba promptvba checkboxescombine excel worksheets into one worksheetexcel password sheetcreate formula in excel vbamicrosoft excel nested ifrecording meeting minutes templatexls to xml conversionmultiple if statements vbadifference between operand and operatorvba application.worksheetfunctionexcel macro delete columnarray function in vbavba connect to sql serverprotect excel workbook with passworduse of excel formulas with examplesumif excel examplesvba backcolorunhide all cellsinsert combo box excelhow to hide excel sheet with passwordexcel vba pivotoffset in vbahow do you unprotect an excel spreadsheetvba sql whereexcel vba listbox rowsourceexcel comparison operatorsvba for next stepprotect a worksheet in excelexcel vba adodbexcel sales dashboardvlookup rangehow do i unhide column ahow to merge cells together in excelmacro basics excelhow to unprotect excel workbook 2013excel vba cell widthvlookup ref errorvba sql selectvlookup formula in excel 2007 with examplevlookup explanationhow to unprotect excel workbook 2013sumif function excel 2007vba excel colorvba handbook pdfsample vb codesshortcut of insert row in excel