Text Files and File Dialog : FreeFile « File Path « VBA / Excel / Access / Word






Text Files and File Dialog

 
     Sub WriteFile()
         Dim dDate As Date
         Dim sCustomer As String
         Dim sProduct As String
         Dim dPrice As Double
         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:\Sales.txt"
         iFNumber = FreeFile
         Open sFName For Output As #iFNumber
         lRow = 2
         Do
             With Sheet1
                 dDate = .cells(lRow, 1)
                 sCustomer = .cells(lRow, 2)
                 sProduct = .cells(lRow, 4)
                 dPrice = .cells(lRow, 6)
             End With
             Write #iFNumber, dDate, sCustomer, sProduct, dPrice
             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.Writing to Text Files Using Print
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