How do I copy dates from a different regional format in Excel VBA?

Nacorid

I have a workbook I use to collect relevant data from a different workbook I only have read-rights to. I connected the latter to my own with Excel's built-in function "Data-connection". Copying a worksheet like this and then copying relevant data from the copied sheet is in my case (yes, I tested it) faster (by about 30%) as opposed to opening said workbook and then copying the relevant data directly from source.

The default date format in the source is "dd/mm/yy" (though it obviously adapts to the local date format settings when opening). The copied sheet keeps that date format but when copying the dates with regional settings as "english-US" Excel assumes the source format to be "yy/mm/dd" even though the cells are formatted as "dd/mm/yy".

And here lies the problem: Not all users of my workbook can use the local date format of "dd/mm/yy", some have to use "mm/dd/yy" or "yy/mm/dd".

How can I tell Excel to copy from a specific date format to the local date format when the local format is not always different?


Checking for the local format with Application.International(xlDateOrder)and then changing the dates if the format differs seems like a complicated workaround which I would like to avoid.


The source format is dd/mm/yy after copying with TargetSheet.Cells(TargetRow, TargetColumn).Value = CDate(SourceSheet.Cells(SourceRow, SourceColumn).Value) the format was dd/mm/yy with days as year and year as days.

Vegard

It might depend on how exactly you are copying the data.

In your case it isn't entirely clear to me what your source data looks like, but for instances where typecasting dates with CDate isn't sufficient to make Excel infer the correct format by itself, you should be able to force the correct interpretation using Format:

TargetSheet.Range("A1").Value = _ 
    CDate(Format(SourceSheet.Range("A1").Value, "dd/mm/yy"))

Do note that you might encounter datasets where you'll need to tweak this method (for example data with varying formats or particular formats that are ill suited for Excel's algorithm for understanding the conversion).

In some instances (text conversion maybe), you may have to apply the format afterwards. That is, applying Format to a CDate object instead of the other way around. I haven't looked this up in any significant extent and can't provide specific details or examples, but I am mentioning it so that you'll be on the look-out.

I am also unsure to what extent this approach will work for source formats that are outside the "english-US" format that is standard in VBA.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do Excel VBA Number format options work for users with different regional settings?

How do I to select and copy different parts of excel to excel in VBA?

How do I copy files from the source with different dates than the destination using the Command Line

Formatting Dates & regional setting Excel VBA

How do I copy some specific columns from a given range which is auto filtered in Excel VBA?

How do I copy a range from one workbook to another in excel WITHOUT having to name it in VBA?

How do I use Excel VBA to copy a worksheet from a workbook selected using a file browser?

How do I copy one image to another using Excel VBA

How do I format a list of dates from an array of unix timestamps?

how do I convert column from different characters to 10 characters format in excel?

How do I autopopulate FIRST dates in Excel from a user ID

How do I extract dates from an Excel sheet?

How can I validate or format dates in excel

how do I change date format on a list of dates (and retain that date format when saving to excel via openpyxl)?

How do I conditionally format a date that falls between two dates from a list of start and end dates?

How do I change mirrors in Ubuntu Server from regional to main?

How do i get 7 consecutive dates(dates are different in array) from an array onwards the current date?

How do I filter dates in VBA properly?

How do I change all dates into the same format in R (if columns has bunch of different character formats)?

How do I enter dates in ISO 8601 date format (YYYY-MM-DD) in Excel (and have Excel recognize that format as a date value)?

How do I validate the correct date format pattern? Format patterns, not dates from strings

how to copy from excel to outlook and keep the format?

How do I copy and paste formatting of excel tab to other tabs except a selected sheet using excel VBA

Excel VBA - Date Cells automatically change back to Regional Date Format

How can I copy data(include format, but except formula) from one excel to other excel?

Excel VBA copy paste then format

How do I keep Excel from changing my date format?

How can I make Excel copy my formatted date to another window and keep the format when some dates are full and some are only the year

IsNumeric gives unexpected results in excel/vba for different regional settings