Compile Error: "Sub or Function not defined" - 3-line macro

roc_on_the_rocks

Board Regular
Joined
Jun 6, 2009
Messages
175
Office Version
  1. 365
Platform
  1. Windows
I'm trying to use MATCH to identify the Column number that contains "DL_Error", then I'll use this Column # to select a cell (found Column # & "34"), but I get the error shown in the title of this post.

Here's my current code:

Sub Macro15()
' Macro recorded 6/18/2009 by me
Range(Match("DL_ERROR", "A31:CW31", 0) & "34").Select
End Sub

Please help, thanks!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi,

MATCH() is a worksheet function, not a VBA function, hence the error. If you want to use the MATCH() worksheet function in VBA you can call it via the worksheetfunction object. For example:

Code:
Sub foo()
    Dim lCol As Long
    
    lCol = Application.WorksheetFunction.Match("DL_ERROR", Sheet1.Range("A31:CW31"), 0)
    
End Sub

Hope that helps...
 
Upvote 0
Your problem is 2fold..

1. The RANGE object cannot use a column NUMBER, it must have a column LETTER.
The Cells object can use Either column Number or Letter
Cells(Row#, Column# or letter)

2. to use match, you have to write it like this
Application.Match("DL_ERROR", RANGE("A1:CW31"),0)

Try this

CELLS(34, Application.Match("DL_ERROR", RANGE("A31:CW31"), 0)).Select
 
Upvote 0
Thank you so much Colin_L and jonmo1 for the speedy response. Both solutions worked like a charm!

I sure appreciate the education. I didn't HAVE to use MATCH, but since my VBA skills are limited, it was just trying to make it work.
 
Upvote 0
In the same line of thought, what's this simplest way to select all CELLS between - including - Row 34 and the last populated Row, still in the Column that contains the value "XPTO" (made up).

I'll use this selection to apply a Condition Format, among other things.

Thanks in advance,
 
Upvote 0
MyCol = Application.Match("DL_ERROR", RANGE("A31:CW31"), 0)
LR = Cells(Rows.Count, MyCol).End(xlup).Row
Range(Cells(34,MyCol),Cells(LR,MyCol)).Select
 
Upvote 0
Solution
Two quick questions please: How to:

1) Instead of hard-coding the Column CW in the Application.Match, make that RANGE to go from A31 - including - until all the way to the last populated Column?

Application.Match("DL_ERROR", RANGE("A1:CW31"),0)

2) How to I put a formula to average these cells:
MyCol = Application.Match("DL_ERROR", RANGE("A31:CW31"), 0)
LR = Cells(Rows.Count, MyCol).End(xlup).Row
Range(Cells(34,MyCol),Cells(LR,MyCol)).Select

As always, big thanks in advance,
 
Upvote 0
Can someone please help me?

Two quick questions please: How to:

1) Instead of hard-coding the Column CW in the Application.Match, make that RANGE to go from A31 - including - until all the way to the last populated Column?

Application.Match("DL_ERROR", RANGE("A1:CW31"),0)

2) How to I put a formula to average these cells:
MyCol = Application.Match("DL_ERROR", RANGE("A31:CW31"), 0)
LR = Cells(Rows.Count, MyCol).End(xlup).Row
Range(Cells(34,MyCol),Cells(LR,MyCol)).Select

As always, big thanks in advance,
 
Upvote 0

Forum statistics

Threads
1,215,135
Messages
6,123,238
Members
449,093
Latest member
Vincent Khandagale

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