An Example Using Write # and Input # : FreeFile « File Path « VBA / Excel / Access / Word






An Example Using Write # and Input #

 

Sub TestWriteInput()
    Dim lOutputFile As Long
    Dim rg As range
    Set rg = ThisWorkbook.Worksheets(1).range("a1")
    lOutputFile = FreeFile
    Open "C:\Write Example.txt" For Output As #lOutputFile
    Do Until IsEmpty(rg)
        Write #lOutputFile, rg.value, _
            rg.Offset(0, 1).value, _
            rg.Offset(0, 2).value, _
            rg.Offset(0, 3).value, _
            rg.Offset(0, 4).value, _
            rg.Offset(0, 5).value, _
            rg.Offset(0, 6).value, _
            rg.Offset(0, 7).value
        Set rg = rg.Offset(1, 0)
    Loop

    Set rg = Nothing
    Close lOutputFile

    Dim lInputFile As Long
    Dim v1, v2, v3, v4
    Dim v5, v6, v7, v8

    Set rg = ThisWorkbook.Worksheets(2).range("a1")
    rg.CurrentRegion.ClearContents
    lInputFile = FreeFile
    Open "C:\Write Example.txt" For Input As lInputFile
    Do Until EOF(lInputFile)
        Input #lInputFile, v1, v2, v3, v4, v5, v6, v7, v8
        rg.value = v1
        rg.Offset(0, 1).value = v2
        rg.Offset(0, 2).value = v3
        rg.Offset(0, 3).value = v4
        rg.Offset(0, 4).value = v5
        rg.Offset(0, 5).value = v6
        rg.Offset(0, 6).value = v7
        rg.Offset(0, 7).value = v8
        Set rg = rg.Offset(1, 0)
    Loop

    Set rg = Nothing
    Close lInputFile
End Sub

 








Related examples in the same category

1.Examples of the VBA Open Statement
2.Text Files and File Dialog
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