Writing to Text Files Using Print : FreeFile « File Path « VBA / Excel / Access / Word






Writing to Text Files Using Print

 
     Sub WriteStrings()
         Dim sLine As String
         Dim sFName As String    'Path and name of text file
         Dim iFNumber As Integer    'File number
         Dim lRow As Long     'Row number in worksheet

         sFName = "C:\Strings.txt"
         iFNumber = FreeFile
         Open sFName For Output As #iFNumber
         lRow = 2
         Do
             With Sheet1
                 sLine = Format(.Cells(lRow, 1), "yyyy-mmm-dd") & ";"
                 sLine = sLine & .Cells(lRow, 2) & ";"
                 sLine = sLine & .Cells(lRow, 4) & ";"
                 sLine = sLine & Format(.Cells(lRow, 6), "0.00")
             End With
             Print #iFNumber, sLine
             lRow = lRow + 1
         Loop Until IsEmpty(Sheet1.Cells(lRow, 1))
         Close #iFNumber
     End Sub

 








Related examples in the same category

1.Examples of the VBA Open Statement
2.An Example Using Write # and Input #
3.Text Files and File Dialog
4.Reading Data Strings
5.Flexible Separators and Delimiters
6.uses the delimiter characters to decide the data type of each item and treat it appropriately:
7.Handling Files with Low-Level File Handling