Import multi text to one worksheet vba macro

yazeedot

New Member
Joined
Feb 15, 2018
Messages
1
Hello ,


I have multi text files in one folder

for example

log001.txt
log002.txt
log003.txt


I want to copy the content of these file in one work sheet

so first file content in A
and second file content in B
and third file content in C
etc..
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Code:
Sub SimpleImport()
On Error GoTo ErrorHandler
Dim fn As Integer
Dim RawData As String
Dim rngTarget As Range
Dim TargetRow As Long
Dim x As Integer
Dim strFolder As String
Dim strFileName As String
strFolder = Application.ActiveWorkbook.Path & "\"
For x = 1 To 3  ' set your maximum number of files here
    TargetRow = 0
    Set rngTarget = ActiveSheet.Range("A1")
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    strFileName = strFolder & "log00" & x & ".txt"
    If Dir(strFileName) = "" Then
        MsgBox strFileName & " NOT FOUND"
        Exit Sub
    End If
    
    fn = FreeFile
    Open strFileName For Input As #fn

    Application.StatusBar = "Processing ... " & x
    Do While Not EOF(fn)
        Line Input [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fn"]#fn[/URL] , RawData
       
        rngTarget.Offset(TargetRow, x - 1) = RawData
        TargetRow = TargetRow + 1
    Loop
    Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fn"]#fn[/URL] 
Next
ErrorHandler:
If Err.Number <> 0 Then
    MsgBox RawData & vbCrLf & vbCrLf & Err.Description, vbCritical
End If
Application.StatusBar = False
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
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