VBA help to open .txt files and copy into its respective sheet

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
878
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Looking for some code to help speed up and simplify my process. Currently I have 11 TXT files that I copy into 11 sheets. Each txt file is named something like this Data_LogsXT.txt, where only the last 2 characters will change (so 10 others files). I created each sheet to be named those last 2 characters. An example, sheet name "XT." Looking for a way to open its txt file, copy the data, and paste in cell A1 of its respective sheet then go to the next file and do the same for each sheet/file combo.

1733432242723.png
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
It might be quicker just to try it. Try replacing the vbCrLf with just vbLf.

PS: If you have Notepad++ and you use it to open the file then it should show you.
 
Upvote 0
Ok cool - easy enough. So when I changed to vbLf it seemed to be working. But towards the end it popped this error

1734441381270.png


When debugging it displays "out of memory" at this spot. A few files are large is my volume of data causing us problems?

1734442055816.png


1734442574690.png
 
Upvote 0
Here's another approach. Read each .txt file line by line and write the lines to the sheet in blocks of 100,000 lines (defined by MAX_ARRAY_SIZE).

VBA Code:
Public Sub Import_Data_Logs_To_Sheets3()

    Const MAX_ARRAY_SIZE = 100000
    
    Dim textFilesFolder As String
    Dim textFile As String
    Dim FSO As Object 'Scripting.FileSystemObject
    Dim FSts As Object 'Scripting.TextStream
    Dim r As Long, n As Long
    ReDim lines(1 To MAX_ARRAY_SIZE, 1 To 1) As String
  
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select folder containing .txt files"
        If .Show Then
            textFilesFolder = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
    End With
    
    Set FSO = CreateObject("Scripting.FileSystemObject") 'New Scripting.FileSystemObject
    
    textFile = Dir(textFilesFolder & "Data_Logs_*.txt")  'line endings are LF characters
    While textFile <> vbNullString
        With ActiveWorkbook.Worksheets(Mid(textFile, 12, 2))
            .Cells.Delete
            Set FSts = FSO.OpenTextFile(textFilesFolder & textFile, 1)  'ForReading
            r = 1
            n = 0
            Do Until FSts.AtEndOfStream
                n = n + 1
                lines(n, 1) = FSts.ReadLine
                If n = MAX_ARRAY_SIZE Then
                    .Cells(r, 1).Resize(n).Value = lines
                    r = r + n
                    n = 0
                End If
            Loop
            FSts.Close
            If n > 0 Then
                .Cells(r, 1).Resize(n).Value = lines
            End If
        End With
        textFile = Dir
    Wend
    
    Set FSts = Nothing
    Set FSO = Nothing

    MsgBox "Done"

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,698
Messages
6,180,426
Members
452,981
Latest member
MarkS1234

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