Schroeter91
New Member
- Joined
- Apr 7, 2022
- Messages
- 1
- Office Version
- 365
- Platform
- Windows
Hello,
I found some code to import multiple .txt files into separate sheets in one workbook.
It works, however, the first .txt file it uses opens in a separate workbook and will not close after the macro has finished.
Can someone please help me find the issue.
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
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 wkbAll = ThisWorkbook
Set wkbTemp = Workbooks.Open(FileName:=FilesToOpen(x))
wkbTemp.Sheets(1).Copy
Set wkbAll = ThisWorkbook
wkbTemp.Close (False)
While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(FileName:=FilesToOpen(x))
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(ThisWorkbook.Sheets.Count)
End With
x = x + 1
Wend
Application.ScreenUpdating = True
Sheets("Data Import & Instructions").Select
Range("V5").Select
ActiveCell.Formula2R1C1 = "=TRANSPOSE(SheetNames)"
Range("V5:V30").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
End Sub
I found some code to import multiple .txt files into separate sheets in one workbook.
It works, however, the first .txt file it uses opens in a separate workbook and will not close after the macro has finished.
Can someone please help me find the issue.
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
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 wkbAll = ThisWorkbook
Set wkbTemp = Workbooks.Open(FileName:=FilesToOpen(x))
wkbTemp.Sheets(1).Copy
Set wkbAll = ThisWorkbook
wkbTemp.Close (False)
While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(FileName:=FilesToOpen(x))
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(ThisWorkbook.Sheets.Count)
End With
x = x + 1
Wend
Application.ScreenUpdating = True
Sheets("Data Import & Instructions").Select
Range("V5").Select
ActiveCell.Formula2R1C1 = "=TRANSPOSE(SheetNames)"
Range("V5:V30").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
End Sub