ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   macro-match function (https://www.excelbanter.com/excel-discussion-misc-queries/175224-macro-match-function.html)

yshridhar

macro-match function
 
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




RobN[_2_]

macro-match function
 
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

"yshridhar" wrote in message
...
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






yshridhar

macro-match function
 
Thank you Rob. One more clarification. How to insert msgbox OnError.
With regards
Sreedhar

"RobN" wrote:

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

"yshridhar" wrote in message
...
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







RobN[_2_]

macro-match function
 
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

"yshridhar" wrote in message
...
Thank you Rob. One more clarification. How to insert msgbox OnError.
With regards
Sreedhar

"RobN" wrote:

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

"yshridhar" wrote in message
...
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









yshridhar

macro-match function
 
Thank you Rob. This is what i want.
With regards
Sreedhar

"RobN" wrote:

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

"yshridhar" wrote in message
...
Thank you Rob. One more clarification. How to insert msgbox OnError.
With regards
Sreedhar

"RobN" wrote:

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

"yshridhar" wrote in message
...
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










Dave Peterson

macro-match function
 
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

yshridhar wrote:

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


--

Dave Peterson


All times are GMT +1. The time now is 07:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com