Need Help - Stop excel macro from coverting copy paste date to text format

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?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top