A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Move the Set wkbAll = ActiveWorkbook to a position before x = 1
If you want to process all of the files in a folder, use:
Sub CombineTextFiles()
Dim FilesToOpen
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String
Dim fd as FileDialog
Dim strFolder as String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
sDelimiter = "|"
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.title = "Select the folder that contains the files that you want to save with a thumbnail."
If .Show = -1 Then
strFolder = .SelectedItems(1) & ""
Else
MsgBox "You did not select a folder."
GoTo ExitHandler
End If
End With
strfile = Dir$(strFolder & "*.csv")
Set wkbAll = ActiveWorkbook
x = 1
While strfile <> ""
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:=False, _
Tab:=False, Semicolon:=False, _
Comma:=False, Space:=False, _
Other:=True, OtherChar:=sDelimiter
End With
wkbTemp.Close SaveChanges:=False
x = x + 1
strfile = Dir$()
Wend
ExitHandler:
Application.ScreenUpdating = True
Set wkbAll = Nothing
Set wkbTemp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
If you want to process all of the files in a folder and not just .csv files, as long as they are all the right type of file (e.g. no .doc, .pdf, etc.) replace
strfile = Dir$(strFolder & "*.csv")
with
strfile = Dir$(strFolder & "*.*")