dd/mm/yyyy vs mm/dd/yyyy Why is one macro confusing these and not the other macro?

PTP86

Board Regular
Joined
Nov 7, 2009
Messages
86
Hi

I know about the classic problem of VBA seeing a date that was in format dd/mm/yyyy and treating it as mm/dd/yyyy instead.
But I've developed 2 macros for manipulating different sets of data and I'm puzzled why one of the macros is being affected by this whilst the other macro isn't.


Dim HistoryArray() as variant
.....
.....
Redim HistoryArray(1 to xxx, 1 to yyy)


Macro 1
Column I & J of the worksheet have cells formatted as *14/03/2012. Those cells contain formulas that result in dates.
Some of those dates are uploaded into the HistoryArray one at a time.
Those dates that were uploaded, plus other data items, are then pasted into another worksheet:
Code:
OutputSheet.Range(Cells(1,1), Cells(RowsRequired,4)) = HistoryArray
There are no calculations or comparisons of dates done in the VBA

Those values are appearing in the output in *14/03/2012 format, just like I want :)


Macro 2 (Note: Transpose is involved in this because I would have wanted the number of rows in the results array to be dynamic, but you can only redim the second dimension)
Columns P and Q have cells formatted as *14/03/2012. Those cells contain formulas that result in dates.
Columns F and G have cells formatted as custom dd-mm-yyyy. Those cells contain raw data values (that's the format of the raw data).
It sorts the worksheet by column F
All data is uploaded into HistoryArray via a transpose operation
Code:
HistoryArray = Application.WorksheetFunction.Transpose(Sheets("History").Range("A1:Q" & LastRow).Value)
Using the array, it does comparisons between the dates that were taken from P,Q,F,G and outputs the relevant ones into a ResultsArray

The ResultsArray is then pasted onto another worksheet via a transpose operation
Code:
OutputSheet.Range("A1").Resize(RowsRequired,11) = Application.WorksheetFunction.Transpose(ResultsArray)


Some dates are appearing in the results as e.g. 01/04/2016 when they should be 04/01/2016 :(


So why is one macro doing this unwanted editing of dates whereas the other macro doesn't?
Is it the transpose that is making the difference?


I've got around this problem with Macro 2 by making the macro format all dates as .NumberFormat = "0" on the worksheet before uploading them to the array, then re-format them back as dd/mm/yyyy when its finished.

I'm feeling like I need to do the same with Macro 1 just to be safe. But it's bugging me why wasn't this a problem with Macro 1?



Thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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