macro-match function

Y

yshridhar

Hello everybody
The following macro i copied from VB help
Sub FindFirst()
myvar = Application.WorksheetFunction.Match(9,
Worksheets(1).Range("A1:A10"), 0)
MsgBox myvar
End Sub

I want to insert a condition, if the number is not available (function
returns error) then exit. Inthis case the macro is returning error 1004.
How to modify the code?
With regards
Sreedhar
 
R

RobN

This would work

Sub FindFirst()
On Error GoTo Endit
myvar = Application.WorksheetFunction.Match(9,
Worksheets(1).Range("A1:A10"), 0)
MsgBox myvar
Endit:
End Sub

Rob
 
Y

yshridhar

Thank you Rob. One more clarification. How to insert msgbox OnError.
With regards
Sreedhar
 
R

RobN

yshridhar,

Hope this is what you're after.

Sub FindFirst()
On Error GoTo Endit
myvar = Application.WorksheetFunction.Match(9,
Worksheets(1).Range("A1:A10"), 0)
MsgBox myvar
Exit Sub
Endit:
MsgBox "No match found!"
End Sub

Rob
 
D

Dave Peterson

You could also use:

Sub FindFirst()
dim myVar as variant
myvar = Application.Match(9, Worksheets(1).Range("A1:A10"), 0)
if iserror(myvar) then
msgbox "Error"
else
MsgBox myvar
end if
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top