EileenJohn
Board Regular
- Joined
- Nov 23, 2016
- Messages
- 53
I have excel A.csv and I tried to copy paste data in excel A to excel B.xlsm
But, when I copy data to excel B, in date column change format to text.
In excel A , the date format is dd-mmm-yyyy (15-12-2016).
In excel B the date format is dd/mm/yyyy (15/12/2016).
Below is my code:
'Force the explicit delcaration of variables
Option Explicit
Dim TimeToRun
Sub UpdateRecord()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
'Specify the path to the folder
MyPath = "C:\Users\Desktop\RECORD"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "" Then MyPath = MyPath & ""
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.csv", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(MyFile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
'Get the next Excel file from the folder
MyFile = Dir
Loop
'Open the latest file
Workbooks.Open MyPath & LatestFile
Dim lastrow As Long, lastcolumn As Long, erow As Long
'Copy
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close
'paste
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.Paste Destination:=Worksheets("sheet1").Range(Cells(erow, 1), Cells(erow, lastcolumn))
'removeduplicate
Cells.Select
Range("Q10").Activate
ActiveSheet.UsedRange.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6 _
, 7, 8, 9, 10, 11, 12, 13, 14, 15), Header:=xlYes
Application.DisplayAlerts = True
End Sub
I've tried:
1. put this code --> Columns("F:F"). NumberFormat ="dd-mm-yyyy" - not working
2. Right click-Format cells - Date
When i click on general or number, the date stay as 15/12/2016. - not working
3. But when i double click on the cells the date change to 15-12-2016 and when i copy paste without using macro.
What should i do? which part of the code should i change?
But, when I copy data to excel B, in date column change format to text.
In excel A , the date format is dd-mmm-yyyy (15-12-2016).
In excel B the date format is dd/mm/yyyy (15/12/2016).
Below is my code:
'Force the explicit delcaration of variables
Option Explicit
Dim TimeToRun
Sub UpdateRecord()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
'Specify the path to the folder
MyPath = "C:\Users\Desktop\RECORD"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "" Then MyPath = MyPath & ""
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.csv", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(MyFile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
'Get the next Excel file from the folder
MyFile = Dir
Loop
'Open the latest file
Workbooks.Open MyPath & LatestFile
Dim lastrow As Long, lastcolumn As Long, erow As Long
'Copy
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close
'paste
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.Paste Destination:=Worksheets("sheet1").Range(Cells(erow, 1), Cells(erow, lastcolumn))
'removeduplicate
Cells.Select
Range("Q10").Activate
ActiveSheet.UsedRange.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6 _
, 7, 8, 9, 10, 11, 12, 13, 14, 15), Header:=xlYes
Application.DisplayAlerts = True
End Sub
I've tried:
1. put this code --> Columns("F:F"). NumberFormat ="dd-mm-yyyy" - not working
2. Right click-Format cells - Date
When i click on general or number, the date stay as 15/12/2016. - not working
3. But when i double click on the cells the date change to 15-12-2016 and when i copy paste without using macro.
What should i do? which part of the code should i change?