VBA: import text from multiple text files into rows of a single excel worksheet in column B with given addresses in column A

azov5

New Member
Joined
Dec 27, 2018
Messages
40
VBA: import text from multiple text files into rows of a single excel worksheet in column B with given addresses in column A
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi, please share the text file to see how data is available. Also share the expected output in Excel file.

Thanks,
Saurabh
 
Upvote 0
Hi,

Check below code.
Before using code, add reference as below:
Tool -> References -> Microsoft scripting RunTime
VBA Code:
Sub txtToExcel() 
    
    Dim objFSO As FileSystemObject
    Dim objTS As TextStream
    Dim filePath As String
    Dim rowno As Integer
    Dim strFile As String
    
    Application.ScreenUpdating = False
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    For rowno = 2 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
        filePath = ActiveSheet.Range("A" & rowno)
        If Dir(filePath) <> vbNullString Then
            Set objTS = objFSO.OpenTextFile(filePath, ForReading, False, TristateUseDefault)
            ActiveSheet.Range("B" & rowno).Value = objTS.ReadAll
            objTS.Close
        Else
            ActiveSheet.Range("B" & rowno).Value = "File Not Found"
        End If
    Next
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Hi, Please share what the sheet contains using XL2BB.

Make sure file exists in the path you specified in column A of the sheet.
Which line is highlighted when you debug the error ?
 
Upvote 0
Worked. file-path was the issue. Thank you.
Through a separate VBA can I combine column B cell data with Column E cell data?
and save the combined data as a new random file name (given in row cells of columns C) in the given folder location on the desktop (Given directory location in row cells of columns D)?
 
Upvote 0
Hi,

Check below code.
Before using code, add reference as below:
Tool -> References -> Microsoft scripting RunTime
VBA Code:
Sub txtToExcel()
   
    Dim objFSO As FileSystemObject
    Dim objTS As TextStream
    Dim filePath As String
    Dim rowno As Integer
    Dim strFile As String
   
    Application.ScreenUpdating = False
    Set objFSO = CreateObject("Scripting.FileSystemObject")
   
    For rowno = 2 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
        filePath = ActiveSheet.Range("A" & rowno)
        If Dir(filePath) <> vbNullString Then
            Set objTS = objFSO.OpenTextFile(filePath, ForReading, False, TristateUseDefault)
            ActiveSheet.Range("B" & rowno).Value = objTS.ReadAll
            objTS.Close
        Else
            ActiveSheet.Range("B" & rowno).Value = "File Not Found"
        End If
    Next
    Application.ScreenUpdating = True

End Sub
If the file starts with "=" or any other special character or number then the VBA does not work. Can this be done?
 
Upvote 0
File name cant contain special characters.
 
Upvote 0
File name cant contain special characters.
not the file name. I mean if the text in the file starts with a special character then the VBA does not work and I have some special characters before the starting text in some files. Help.
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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