VBA Match problem

liampog

Active Member
Joined
Aug 3, 2010
Messages
308
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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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,214,641
Messages
6,120,688
Members
448,978
Latest member
rrauni

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