A little doosey Date problem

mikemcbain

Board Regular
Joined
Nov 14, 2005
Messages
152
Office Version
  1. 365
Platform
  1. Windows
G;day Magicians

I have a problem that has thwarted me and ChatGPT for 4 hours today?

I am using Excel 365 and I have a csv file on my computer that has dates in Col A with a Header called DATE
I can copy and paste the contents of this file into an xlsm spreadsheet and the date format stays the same ie. 02/04/2013
However if I use a macro or VBA to copy paste or transfer the data the date comes out as 04/02/2023?
And then when I check back to the csv file it has also been changed to that format so I must create it all over again.

Can anyone offer suggestions about what is going on and how to overcome it?
We have tried text to columns, checked the Option settings and numerous other things.

With high hopes

Old Mike.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi,

You should try to Clean your cells in Column A to get rid of spaces and potential invisible characters ...

A good test to check everything is fine is to temporarily format General and make sure you do get Numbers to work with ...
 
Upvote 0
Hi,

You should try to Clean your cells in Column A to get rid of spaces and potential invisible characters ...

A good test to check everything is fine is to temporarily format General and make sure you do get Numbers to work with ...
Thank you James good advice but still does not help me to find out why it is occurring?

Mike.
 
Upvote 0
All VBA is in the American date format MM/DD/YYYY. Bring it into VBA as CLng(your cell reference) and output it the same.

If you have already put it in the cells use Text to Columns with the setting MDY then format it as desired
 
Upvote 0
All VBA is in the American date format MM/DD/YYYY. Bring it into VBA as CLng(your cell reference) and output it the same.

If you have already put it in the cells use Text to Columns with the setting MDY then format it as desired
Hi Mark

Many thanks that sure sounds like the reason for my problem.

Would you mind elaborating on your term "CLng" I am not VBA competent and I just keep prompting ChatGPT for my VBA.

Mike.
 
Upvote 0
Use it like CLng(CDate(Range("A2").Value))

The code below gives the results in column C of the table for me in the UK (dd/mm/yyyy format) when column A are text dates
VBA Code:
Sub xxxx()
A = Range("A1").Value
B = Range("A2").Value
C = CLng(CDate(Range("A1").Value))
D = CLng(CDate(Range("A2").Value))
Range("C1").Value = A
Range("C2").Value = B
Range("C3").Value = C
Range("C4").Value = D
End Sub
Book1 (version 1).xlsb
ABC
14/2/202302/04/2023
211/3/202303/11/2023
304/02/2023
411/03/2023
5
Sheet5


You might get away with just
VBA Code:
CDate(Range("A2").Value)
I just keep prompting ChatGPT for my VBA
Not a good idea as it makes some things up ;) better to ask a question on a forum like this one until it gets better
 
Last edited:
Upvote 0
Solution
Use it like CLng(CDate(Range("A2").Value))

The code below gives the results in column C of the table for me in the UK (dd/mm/yyyy format) when column A are text dates
VBA Code:
Sub xxxx()
A = Range("A1").Value
B = Range("A2").Value
C = CLng(CDate(Range("A1").Value))
D = CLng(CDate(Range("A2").Value))
Range("C1").Value = A
Range("C2").Value = B
Range("C3").Value = C
Range("C4").Value = D
End Sub
Book1 (version 1).xlsb
ABC
14/2/202302/04/2023
211/3/202303/11/2023
304/02/2023
411/03/2023
5
Sheet5


You might get away with just
VBA Code:
CDate(Range("A2").Value)

Not a good idea as it makes some things up ;) better to ask a question on a forum like this one until it gets better
Hi Mark

Many thanks for that explanation and code.
Perfect for my purposes.
There are some aspects of Excel and other programs that ChatGPT is good at but complicated VBA code is not one of them.
You are correct though I have never ever had a failure asking a question on this Forum.

Best regards

Mike.
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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