Copy text files from text file to Worksheet With VBA

Abhishekghorpade

Board Regular
Joined
Oct 3, 2018
Messages
78
I have multiple text files saved in the Path “C:\Users\E5554593\Desktop\New folder”. I need to import the data from text files to excel. Each file data should come in separate tabs, Tab name will be same as text file name.
Can anyone help me out with this please.. Its consuming lot of time
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Please run in debug. Open the macro, and click F8 and step through each line of code. You can hover the mouse over variables to see their value after a step is executed note where an error occurs.
Since it's doing one file it's probably getting to the loop, make sure the file name changes in MyFile which is just prior to the loop. Also check vTabName - it's stripping off ".txt" prior to changing the name of a new tab.
If you already have an existing tab name and it tries to create a new one there will be an error, there was no error routine here - would have to know how you want to handle them, if a tab name exists data to bottom of existing? All sorts of questions on that.
 
Upvote 0
You can add a filter to exclude those values - here's the code revised to filter column A so it does not include rows that have PAGE or GLOBAL PLAN in column A:
Code:
Sub Macro1()
'   HouseKeeping
    MyPath = "%USERPROFILE%\Desktop\New Folder\"
    MyExt = "*.txt"
    ThisBook = ActiveWorkbook.Name
    MyFile = Dir(MyPath & MyExt)
    Do While MyFile <> ""
        Workbooks.OpenText Filename:=MyPath & MyFile, Origin:=437, _
            StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(14 _
            , 1), Array(33, 1), Array(50, 1), Array(75, 1), Array(82, 1), Array(86, 1)), _
            TrailingMinusNumbers:=True
        Cells.Select
        Selection.Columns.AutoFit
        vTabName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)
        vLR = ActiveSheet.UsedRange.Rows.Count
        Selection.AutoFilter
        ActiveSheet.Range("A1:G" & vLR).AutoFilter Field:=1, Criteria1:= _
            "<>*Page*", Operator:=xlAnd, Criteria2:="<>*Global*"
        Range("A1:G" & vLR).Select
        Selection.Copy
        Windows(ThisBook).Activate
        Sheets.Add After:=ActiveSheet
        vx = ActiveSheet.Name
        Sheets(vx).Name = vTabName
        Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Windows(vTabName & ".txt").Activate
        Application.DisplayAlerts = False
        ActiveWindow.Close savechanges:=False
        Application.DisplayAlerts = True
        MyFile = Dir
    Loop
End Sub
 
Last edited:
Upvote 0
Thank you so much its working as expected. I have on more concern, This report is going to be used by many people and chance of spoiling the report is high.. Can you make it as read only/password protected.. So that no one can edit or see the coding yet still use it
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,639
Members
449,093
Latest member
Ahmad123098

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