XlsmGames
New Member
- Joined
- Feb 19, 2013
- Messages
- 6
Oh silly Match function. How you vex me so!
I am using WorksheetFunction.Match() to identify the location of values in a range. Some of the values in the range are strings. Some of the values are dates. And guess what? Match chokes on the dates. It's clearly a type mistmatch issue. However, I cannot figure out a workaround since values in the range could be strings or dates. Can I cast the entire range using CDate? How would that affect the string values?
A simplified version of my code is:
Dim ColNumInTarget As Double
Dim strTextToMatchAs String
Dim rgTargetHeaderRow As Variant
Dim i as Integer
set rgTargetHeaderRow = ActiveSheet.Range("A1:A8")
strTextToMatch = Worksheet("Sheet3").Cells(1, i).Value
ColNumInTarget = WorksheetFunction.Match(strTextToMatch, rgTargetHeaderRow, 0)
rgTargetHeaderRow has values:[TABLE="width: 525"]
<tbody>[TR]
[TD="class: xl74, width: 64"]Q1 Spend[/TD]
[TD="class: xl75, width: 64"]Q2 Spend[/TD]
[TD="class: xl75, width: 64"]Q3 Spend[/TD]
[TD="class: xl76, width: 64"]Q4 Spend[/TD]
[TD="class: xl77, width: 68"]Oct-12[/TD]
[TD="class: xl78, width: 68"]Nov-12[/TD]
[TD="class: xl78, width: 68"]Dec-12[/TD]
[TD="class: xl78, width: 65"]Jan-1[/TD]
[/TR]
</tbody>[/TABLE]
strTextToMatch pulls values from another sheet. Those values need to be matched with values in the range rgTargetHeaderRow. If strTextToMatch = "Q1 Spend", I can get a match. If strTextToMatch = "10/1/2012", I cannot match Oct-12 and I get a 1004 error -- "Unable to get the Match property of the Worksheet Function class".
Long time lurker, first timer poster. If you (the MrExcel community) will accept some flattery, this really is the best excel board on the interwebs. Great explanations and lots of elegant solutions!
I am using WorksheetFunction.Match() to identify the location of values in a range. Some of the values in the range are strings. Some of the values are dates. And guess what? Match chokes on the dates. It's clearly a type mistmatch issue. However, I cannot figure out a workaround since values in the range could be strings or dates. Can I cast the entire range using CDate? How would that affect the string values?
A simplified version of my code is:
Dim ColNumInTarget As Double
Dim strTextToMatchAs String
Dim rgTargetHeaderRow As Variant
Dim i as Integer
set rgTargetHeaderRow = ActiveSheet.Range("A1:A8")
strTextToMatch = Worksheet("Sheet3").Cells(1, i).Value
ColNumInTarget = WorksheetFunction.Match(strTextToMatch, rgTargetHeaderRow, 0)
rgTargetHeaderRow has values:[TABLE="width: 525"]
<tbody>[TR]
[TD="class: xl74, width: 64"]Q1 Spend[/TD]
[TD="class: xl75, width: 64"]Q2 Spend[/TD]
[TD="class: xl75, width: 64"]Q3 Spend[/TD]
[TD="class: xl76, width: 64"]Q4 Spend[/TD]
[TD="class: xl77, width: 68"]Oct-12[/TD]
[TD="class: xl78, width: 68"]Nov-12[/TD]
[TD="class: xl78, width: 68"]Dec-12[/TD]
[TD="class: xl78, width: 65"]Jan-1[/TD]
[/TR]
</tbody>[/TABLE]
strTextToMatch pulls values from another sheet. Those values need to be matched with values in the range rgTargetHeaderRow. If strTextToMatch = "Q1 Spend", I can get a match. If strTextToMatch = "10/1/2012", I cannot match Oct-12 and I get a 1004 error -- "Unable to get the Match property of the Worksheet Function class".
Long time lurker, first timer poster. If you (the MrExcel community) will accept some flattery, this really is the best excel board on the interwebs. Great explanations and lots of elegant solutions!