VBA Match problem

liampog

Active Member
Joined
Aug 3, 2010
Messages
316
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I have a master workbook that opens another workbook. On the opened workbook, there is a date.

I want to find the column in the master workbook that contains this date and I'm having an issue with the following line of code:

Code:
Week_Column = Application.WorksheetFunction.Match(WeekEndingDate, Workbooks(Report_Filename).Sheets("Sheet1").Range("Setup_DatesList"), 0)

Report_Filename is the master workbook. WeekEndingDate is taken from the opened workbook. It's actually a date extracted from a text string so I did this:

Code:
WeekEndingDate = DateValue(Mid(FindText, Application.WorksheetFunction.Find(",", FindText) - 10, 10))

I believe DateValue is the correct thing to use to turn the text version of the date into a real date value.

The Range("Setup_DatesList") is a named range of dates that are derived from formulas rather than actual input, but I don't believe this should be causing the problem.

I'm getting the standard runtime error 1004, unable to get the Match property of the WorksheetFunction class.

Can anyone tell me what I'm doing wrong?

Thanks
Liam
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Untested but maybe something like this:
Use 'Application.Match'.

Code:
Dim Week_Column As Variant
Week_Column = Application.Match(WeekEndingDate, Workbooks(Report_Filename).Sheets("Sheet1").Range("Setup_DatesList"), 0)

If IsNumeric(Week_Column) Then
'do something here if match found
End If
 
Upvote 0
Untested but maybe something like this:
Use 'Application.Match'.

Code:
Dim Week_Column As Variant
Week_Column = Application.Match(WeekEndingDate, Workbooks(Report_Filename).Sheets("Sheet1").Range("Setup_DatesList"), 0)

If IsNumeric(Week_Column) Then
'do something here if match found
End If


Hi

That doesn't work unfortunately. It creates a Type mismatch error message.

Could it be to do with the date values in the Range("Setup_DatesList") being as a result of formulas rather than actual dates?

Am I correct in using DateValue to convert a date (in dd/mm/yyyy format) extracted from a text string to an actual date numerical value to use in the Match formula?



Thanks
Liam
 
Upvote 0
Code:
That doesn't work unfortunately.  It creates a Type mismatch error message.
Did you Dim Week_Column As Variant or Long?
It should be variant.
 
Upvote 0
Hi

I actually did some more digging and found a reference that says it's better to use cLng() around dates.

So the following works!

Code:
Week_Column = Application.Match(CLng(WeekEndingDate), Workbooks(Report_Filename).Sheets("Sheet1").Range("Setup_Dates"), 0)

Thanks for your help though.
 
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,659
Members
452,992
Latest member
TokugawaIesuma

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