Hi everyone!
I got a VBA code that can work successfully to import multiple text files to separated sheets.
However, it creates a new workbook, say Book1, while I expect to load all new imported sheets to the current workbook which I am running this VBA code.
So, I hope someone could take a look on the codes and change where it is needed to meet this requirement.
Thank you very much!
I got a VBA code that can work successfully to import multiple text files to separated sheets.
However, it creates a new workbook, say Book1, while I expect to load all new imported sheets to the current workbook which I am running this VBA code.
So, I hope someone could take a look on the codes and change where it is needed to meet this requirement.
Thank you very much!
VBA Code:
Sub Import_Text_Files()
Dim FilesToOpen
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String
Application.ScreenUpdating = False
sDelimiter = "|"
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Text Files (*.txt), *.txt", _
MultiSelect:=True, Title:="Text Files to Open")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
End If
x = 1
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
wkbTemp.Sheets(1).Copy
Set wkbAll = ActiveWorkbook
wkbTemp.Close (False)
wkbAll.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=False, Semicolon:=False, _
Comma:=True, Space:=True, _
Other:=False, OtherChar:="|"
x = x + 1
While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=False, Semicolon:=False, _
Comma:=True, Space:=True, _
Other:=False, OtherChar:=sDelimiter
End With
x = x + 1
Wend
Application.ScreenUpdating = True
End Sub