Hi there.
Like many before me, I’m having issues with Excel converting dates to the US format when importing files to Excel via macro. I’ve found various solutions that claim to work but I am stuck and have been unable to find a solution for my application. I’m certainly no expert with macros and have only cobbled this together from finding other examples posted online, so may require some guidance in what I need to fix.
The purpose of the macro is to import multiple files (.dat files) into a single workbook and save each file as its own worksheet.
Like many before me, I’m having issues with Excel converting dates to the US format when importing files to Excel via macro. I’ve found various solutions that claim to work but I am stuck and have been unable to find a solution for my application. I’m certainly no expert with macros and have only cobbled this together from finding other examples posted online, so may require some guidance in what I need to fix.
The purpose of the macro is to import multiple files (.dat files) into a single workbook and save each file as its own worksheet.
Code:
Sub CombineTextFiles()
'updateby Extendoffice 20151015
Dim xFilesToOpen As Variant
Dim I As Integer
Dim xWb As Workbook
Dim xTempWb As Workbook
Dim xDelimiter As String
Dim xScreen As Boolean
On Error GoTo ErrHandler
xScreen = Application.ScreenUpdating
Application.ScreenUpdating = False
xDelimiter = "|"
xFilesToOpen = Application.GetOpenFilename("Text Files (*.DAT), *.DAT", , "Import Multiple Files", , True)
If TypeName(xFilesToOpen) = "Boolean" Then
MsgBox "No files were selected", , "Import Multiple Files"
GoTo ExitHandler
End If
I = 1
Set xTempWb = Workbooks.Open(xFilesToOpen(I))
xTempWb.Sheets(1).Copy
Set xWb = Application.ActiveWorkbook
xTempWb.Close False
xWb.Worksheets(I).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Other:=False, OtherChar:="|"
Do While I < UBound(xFilesToOpen)
I = I + 1
Set xTempWb = Workbooks.Open(xFilesToOpen(I))
With xWb
xTempWb.Sheets(1).Move after:=.Sheets(.Sheets.Count)
.Worksheets(I).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=True, Space:=False, _
Other:=False, OtherChar:=xDelimiter
End With
Loop
ExitHandler:
Application.ScreenUpdating = xScreen
Set xWb = Nothing
Set xTempWb = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description, , "Import Multiple Files"
Resume ExitHandler
End Sub