Default300
Board Regular
- Joined
- Jul 13, 2009
- Messages
- 83
Hi. I've run into another problem! Baffling one this time!!
I have an Array.
I think it is an Array contained within a Variant Variable, rather than an Array of Variant DataType.
It has 2 Dimensions.
I want to output / paste the entire contents onto a Worksheet Range.
The Range in question has identical dimensions, in the correct orientation.
The Format of all Cells in this Range is initially General and not deliberately changed by any VBA code (but see below).
What works:
Each "Column" or "Field" of Data in the Array is transferred to the correct Column Range on the Worksheet.
Problem:
One of the Array "Fields" holds Date Data.
When transferred to the Worksheet, results are inconsistent...
Each Array Element displays either syndrome (a) or (b), when transferred to the Worksheet, depending on the value of the Date (dd) Element when still in the Array.
Dates 01 - 12 (inclusive): appearance and values are as per group (a).
Dates 13 - 31 (inclusive): appearance and values are as per group (b).
Group (a)
Alignment Appearance: Right
Alignment Setting (in Excel UI's Cell Format dialog): General
Number Format: Date (*14/03/2001)
Values: changed (month swapped with date)
Group (a) seems to have been automatically reformatted, presumably by Excel. I'm fine with the Alignment and Number Format (both correct for a date, I believe). The worrying thing is that the value has changed, making it unuseable. The formulae (MONTH and DAY) return the opposite of what is required.
Example
Array Value = 01/02/2010
Cell (A1) Value = 02/01/2010
Cell (B1) Formula = DAY(A1)
Cell (B1) Value = 02
Cell (C1) Formula = MONTH(A1)
Cell (C1) Value = 01
Group (b)
Alignment Appearance: Left
Alignment Setting (in Excel UI's Cell Format dialog): General
Number Format: General
Values: correct (date to left of month as it should be).
Values useable (formulae return correct month and date, from Worksheet cell, which is also correct relative to array value).
Group (b) does NOT seem to have been automatically reformatted by Excel. It's great that it is useable, but slightly worrting that it does not appear to have been recognised as a date.
Example
Array Value = 01/02/2010
Cell (A1) Value = 01/01/2010
Cell (B1) Formula = DAY(A1)
Cell (B1) Value = 01
Cell (C1) Formula = MONTH(A1)
Cell (C1) Value = 02
Number Format and Alignment Setting were checked with Excel UI's Cell Format dialog.
Values were checked using formulae [ =MONTH(A1) ], and [ =DAY(A1) ]. So it is definitely not just the format that has changed.
In VBA, I checked each Element of that "Field" as follows.
Dates and Months are displayed in correct order in the Immediate Window.
IsDate returns True for each Element.
TypeName returns String as the DataType for each Element. (I don't think this should be a problem, though I'm not sure why it is a String. The Format of the Cell which was originally read into the Original Array was: Date (*14/03/2001).
The Current Array is a Transposed version of the Original Array, created using this code:
The Orginal Array was Dimensioned as follows:
The Original Array copied Values from the Worksheet using this Code:
I also tried this with the same result (though I'd love to know which is more correct):
The Current Array copies Values to the Worksheet using this Code.
NB: The entire 5 "Field" Array is transferred as one, to a 5 Column range (NOT Column by Column):
Incidentally the Locale I'm using (in Control Panel's Regional & Language Settings) uses Short Date: 20/06/2010 and Long Date: 20 June 2010.
Excel's Option Setting for "Extend List Formats and Formulas" is True / On. The cell above the column in question (the header) is also formatted as General and contains text.
Workaround:
Before Copying the Values to the Worksheet, Format Cells as Text.
After Copying Format Cells as Date (*14/03/2001).
I can do this if necessary, but am concerned that the results I'm seeing are symptomatic of an underlying error on my part. I don't want to mask a problem with symptomatic relief if I can cure it completely and avoid it in the future.
Clue:
My deduction is that the fact that a year has 12 months must have something to do with Excel's decision to treat dates for 01-12 differently to those from 13-31.
I have an Array.
I think it is an Array contained within a Variant Variable, rather than an Array of Variant DataType.
It has 2 Dimensions.
I want to output / paste the entire contents onto a Worksheet Range.
The Range in question has identical dimensions, in the correct orientation.
The Format of all Cells in this Range is initially General and not deliberately changed by any VBA code (but see below).
What works:
Each "Column" or "Field" of Data in the Array is transferred to the correct Column Range on the Worksheet.
Problem:
One of the Array "Fields" holds Date Data.
When transferred to the Worksheet, results are inconsistent...
Each Array Element displays either syndrome (a) or (b), when transferred to the Worksheet, depending on the value of the Date (dd) Element when still in the Array.
Dates 01 - 12 (inclusive): appearance and values are as per group (a).
Dates 13 - 31 (inclusive): appearance and values are as per group (b).
Group (a)
Alignment Appearance: Right
Alignment Setting (in Excel UI's Cell Format dialog): General
Number Format: Date (*14/03/2001)
Values: changed (month swapped with date)
Group (a) seems to have been automatically reformatted, presumably by Excel. I'm fine with the Alignment and Number Format (both correct for a date, I believe). The worrying thing is that the value has changed, making it unuseable. The formulae (MONTH and DAY) return the opposite of what is required.
Example
Array Value = 01/02/2010
Cell (A1) Value = 02/01/2010
Cell (B1) Formula = DAY(A1)
Cell (B1) Value = 02
Cell (C1) Formula = MONTH(A1)
Cell (C1) Value = 01
Group (b)
Alignment Appearance: Left
Alignment Setting (in Excel UI's Cell Format dialog): General
Number Format: General
Values: correct (date to left of month as it should be).
Values useable (formulae return correct month and date, from Worksheet cell, which is also correct relative to array value).
Group (b) does NOT seem to have been automatically reformatted by Excel. It's great that it is useable, but slightly worrting that it does not appear to have been recognised as a date.
Example
Array Value = 01/02/2010
Cell (A1) Value = 01/01/2010
Cell (B1) Formula = DAY(A1)
Cell (B1) Value = 01
Cell (C1) Formula = MONTH(A1)
Cell (C1) Value = 02
Number Format and Alignment Setting were checked with Excel UI's Cell Format dialog.
Values were checked using formulae [ =MONTH(A1) ], and [ =DAY(A1) ]. So it is definitely not just the format that has changed.
In VBA, I checked each Element of that "Field" as follows.
Code:
[FONT=Courier New]Dim lngLoop1 As Long
For lngLoop1 = LBound(varContactRecordsTransposed, 1) To _
UBound(varContactRecordsTransposed, 1)
Debug.Print varContactRecordsTransposed(lngLoop1, 4); Tab(14); _
IsDate(varContactRecordsTransposed(lngLoop1, 4)); Tab(22); _
TypeName(varContactRecordsTransposed(lngLoop1, 4))
Next lngLoop1[/FONT]
IsDate returns True for each Element.
TypeName returns String as the DataType for each Element. (I don't think this should be a problem, though I'm not sure why it is a String. The Format of the Cell which was originally read into the Original Array was: Date (*14/03/2001).
The Current Array is a Transposed version of the Original Array, created using this code:
Code:
[FONT=Courier New]Dim varContactRecordsTransposed As Variant
Let varContactRecordsTransposed = Application.WorksheetFunction.Transpose(varContactRecords())[/FONT]
Code:
[FONT=Courier New]Dim varContactRecords()
ReDim varContactRecords(1 To 5, 1 To lngSumOfCellsContainingContacts)[/FONT]
Code:
[FONT=Courier New]Case 4 'ContactDateDDMMYYY
Let varContactRecords(bytLoop2, lngNumberOfContactRecordsFound) = _
Intersect(.EntireColumn, _
Worksheets(strNameOfWorksheetDataEntry) _
.Range("namSheetDataEntryContactsMainSupraHeaderRowDateFullNonMerged"))[COLOR=Red][B].Value[/B][/COLOR][/FONT]
Code:
[FONT=Courier New]Case 4 'ContactDateDDMMYYY
Let varContactRecords(bytLoop2, lngNumberOfContactRecordsFound) = _
Intersect(.EntireColumn, _
Worksheets(strNameOfWorksheetDataEntry) _
.Range("namSheetDataEntryContactsMainSupraHeaderRowDateFullNonMerged"))[/FONT]
NB: The entire 5 "Field" Array is transferred as one, to a 5 Column range (NOT Column by Column):
Code:
[FONT=Courier New]Set rngDestinationRange = _
rngOffsetAnchor _
.Offset(lngOffsetRows, lngOffsetCols).Resize( _
UBound(varContactRecordsTransposed, 1) - LBound(varContactRecordsTransposed, 1) + 1, _
UBound(varContactRecordsTransposed, 2) - LBound(varContactRecordsTransposed, 2) + 1)
[B]Let rngDestinationRange = varContactRecordsTransposed[/B][/FONT]
Excel's Option Setting for "Extend List Formats and Formulas" is True / On. The cell above the column in question (the header) is also formatted as General and contains text.
Workaround:
Before Copying the Values to the Worksheet, Format Cells as Text.
After Copying Format Cells as Date (*14/03/2001).
I can do this if necessary, but am concerned that the results I'm seeing are symptomatic of an underlying error on my part. I don't want to mask a problem with symptomatic relief if I can cure it completely and avoid it in the future.
Clue:
My deduction is that the fact that a year has 12 months must have something to do with Excel's decision to treat dates for 01-12 differently to those from 13-31.