G
gabejessee
Hi everyone,
I'm trying import all the text files in a specific folder into excel via macro, and I've ran into a snag.
Here is my macro:
Sub TxtImporter()
Dim f As String, flPath As String
Dim i As Long, j As Long
Dim ws As Worksheet
Dim t As Integer
Application.DisplayAlerts = False
Application.ScreenUpdating = False
flPath = ThisWorkbook.Path & Application.PathSeparator
i = ThisWorkbook.Worksheets.Count
j = Application.Workbooks.Count
f = Dir(flPath & "*.txt")
t = 0
Do Until f = ""
ActiveCell.Offset(rowOffset:=0, columnOffset:=t).Activate
Workbooks.OpenText flPath & f, _
StartRow:=1, DataType:=xlFixedWidth
t = t + 22
i = i + 1
f = Dir
Loop
Application.DisplayAlerts = True
End Sub
The problem is that when I loop around to import the second file cell "A1" is selected despite the ActiveCell.Offset command at the beginning of the loop. ActiveCell puts the cursor into the correct place where I would like to import but then the OpenText command moves it back to "A1" Is the OpenText function limited to only open files from A1? I'm very novice at this so I'm not sure what I need to do to work around this issue. Any help would beappreciated.
Thanks
-G
I'm trying import all the text files in a specific folder into excel via macro, and I've ran into a snag.
Here is my macro:
Sub TxtImporter()
Dim f As String, flPath As String
Dim i As Long, j As Long
Dim ws As Worksheet
Dim t As Integer
Application.DisplayAlerts = False
Application.ScreenUpdating = False
flPath = ThisWorkbook.Path & Application.PathSeparator
i = ThisWorkbook.Worksheets.Count
j = Application.Workbooks.Count
f = Dir(flPath & "*.txt")
t = 0
Do Until f = ""
ActiveCell.Offset(rowOffset:=0, columnOffset:=t).Activate
Workbooks.OpenText flPath & f, _
StartRow:=1, DataType:=xlFixedWidth
t = t + 22
i = i + 1
f = Dir
Loop
Application.DisplayAlerts = True
End Sub
The problem is that when I loop around to import the second file cell "A1" is selected despite the ActiveCell.Offset command at the beginning of the loop. ActiveCell puts the cursor into the correct place where I would like to import but then the OpenText command moves it back to "A1" Is the OpenText function limited to only open files from A1? I'm very novice at this so I'm not sure what I need to do to work around this issue. Any help would beappreciated.
Thanks
-G