Exporting a range to a text file : Excel to Text File « Excel « VBA / Excel / Access / Word






Exporting a range to a text file

 
Sub ExportRange()
    FirstCol = 1
    LastCol = 3
    FirstRow = 1
    LastRow = 3
    
    Open ThisWorkbook.Path & "\textfile.txt" For Output As #1
        For r = FirstRow To LastRow
            For c = FirstCol To LastCol
                Dim vData As Variant
                vData = Cells(r, c).value
                If IsNumeric(vData) Then vData = Val(vData)
                If c <> LastCol Then
                    Write #1, vData;
                Else
                    Write #1, vData
                End If
            Next c
        Next r
    Close #1
End Sub

 








Related examples in the same category

1.TextToColumns Example
2.Importing a text file to a range