Hi,
I'm combining multiple csv files into one excel sheet using VBA.
Dates in all my csv files are set to be DD/MM/YYYY in format
But when they are copied to the excel sheet,
- the date number and the month number are swapped around.
- and the dates that look the same as those in the csv files convert to text (no longer a date).
What am I doing wrong?? How do I make the dates stay the same as in the csv files?
Help please~~~
Example:
Picture 1.
Dates in csv file: 12/01/2021 = 12th January 2021
Picture 2.
Dates in excel after being copied: 1/12/2021 = 1st December 2021
And <13/01/2021>s are no longer a date.
Below is my code that combines csv files.
Sub Combine ()
Dim FolderPath As String
Dim FileName As String
Dim WB As Workbook
FolderPath = "P:\JAY\Downloads"
If FolderPath Like "*[!/]" Then
FolderPath = FolderPath & "/"
End If
FileName = Dir(FolderPath & "*.csv")
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Do While FileName <> vbNullString
Set WB = Workbooks.Open(FolderPath & FileName)
With WB.ActiveSheet.UsedRange
.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).Copy ThisWorkbook.Worksheets("Sheet1").UsedRange.Rows(ThisWorkbook.Worksheets("Sheet1").UsedRange.Rows.Count).Offset(1) .Resize(1)
End With
WB.Close False
FileName = Dir()
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Thank you!
Jay