lookup horizontal...

S

sal21

Have this range D2:CE2 filled with a many value and a My_var filled
with a value
I want to find in this range if value of My_var existis in range
D2:CE2... if existis goto other code1 else goto other code2 how to in
VBA?
 
M

Mike H

Hi,

Right click your sheet, view code and paste this in

Sub nn()
my_var = 99
Set myrange = Range("D2:CE2")
On Error Resume Next
For Each c In myrange
If c.Value = my_var Then
'run code1
Else
'run code2
End If
Next
End Sub

You must consider what you do after you have run code1 or code2 because
execution will return back into this routine. You could do this

If c.Value = my_var Then
'run code1
exit sub
Else
'run code2
exit sub
End If



Mike
 
M

Mike H

Ignore my last post I had an elderly moment, try this instead

Sub nn()
my_var = 999
Set myrange = Range("D2:CE2")
On Error Resume Next
For Each c In myrange
If c.Value = my_var Then
code1
Exit Sub
End If
Next
code2
End Sub

Sub code1()
MsgBox 1
End Sub


Sub code2()
MsgBox 2
End Sub
 
S

sal21

Ignore my last post I had an elderly moment, try this instead

Sub nn()
my_var = 999
Set myrange = Range("D2:CE2")
On Error Resume Next
For Each c In myrange
    If c.Value = my_var Then
        code1
        Exit Sub
    End If
Next
code2
End Sub

Sub code1()
         MsgBox 1
End Sub

Sub code2()
      MsgBox 2
End Sub









- Mostra testo tra virgolette -

Excellent code!
Tks.
 

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