Read/Write between Excel and Text Files

nmk34

New Member
Joined
Apr 12, 2022
Messages
45
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I am writing a table of 8 columns from Excel 360 to Text file and reading the same columns back to same range in excel. the data in excel left justify to get the data columns in the TextFile to line up. when i read the data back, the data looks way off(possibly like each column separated by the TAB) and the commas show up in excel. when writing to the text file i use the commas to know the end of each field.
is it possible to write to a text file for example sheet range ("A2:D14") as table or columns where the top row is the headers, and the data read back into excel using other sub will look lined up as we started with before writing to the text file.?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
It sounds like you're making a CSV file on export, but with extra spaces or tabs to make it look a bit nicer as plain monospaced text. If it's a good CSV file, then maybe you could use Power Query (Get Data -> CSV) to get the data back in properly.
 
Upvote 0
ReadTextFile.JPG
 

Attachments

  • excel_sheet1.JPG
    excel_sheet1.JPG
    62.7 KB · Views: 10
  • writingToTextFile.JPG
    writingToTextFile.JPG
    87.3 KB · Views: 11
Upvote 0
Do you need that final comma on each line in the text file? It seems unnecessary and it's causing you problems when you bring it back in.
 
Upvote 0
Even if you leave that final comma on, you should be able to address it like this:

VBA Code:
Public Sub ReadDataFromTextFile()

    Dim i As Long, j As Long, LineText As String, CellValue As String
    'Open "C:\mytxtfile.txt" For Input As #1
    Open ThisWorkbook.Path & "\ProductTable1.txt" For Input As #1
    'Open ThisWorkbook.Path & "\mytextfile.txt" For Input As #1
    i = 2
    While Not EOF(1)
        Line Input #1, LineText
        Dim arr As Variant
        
        arr = Split(CStr(LineText), ", ") ' splitting text based on commas
        ' str = VBA.Split(ActiveCell.Value, vbCrLf) splitting text based on cariage return
        
        For j = 1 To 8 ' max number of column in the excel sheet that will have data from text file
            CellValue = Trim(arr(j - 1)) ' Trim to get rid of extra spaces
            If Right(CellValue, 1) = "," Then
                CellValue = Mid(CellValue, 1, Len(CellValue) - 1) ' Remove final comma
                ActiveSheet.Cells(i, j).Value = CellValue
            Else
                ActiveSheet.Cells(i, j).Value = CellValue
                End If
            Next j
            i = i + 1
        Wend
    Close #1

    End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top