Lookups in Vba

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
So I have a code set up which emails the data in a worksheet ("Sheet2"). However I need to set up the .to address so that it is dependant on different values on the sheet. ("J7") & ("J9").

I can hard code this fine.

Code:
Dim Recipient As String
If Range("J7") = "VALUE1" Then
    Recipient = "Email1"
Else
     If Range("J7") = "VALUE2" Then
         Recipient = "Email2"
...etc

However I would prefer to use a lookup from the sheet ("LookupLists"), so something like

Code:
If Sheets("Sheet2").Range("J7").Value = Sheets("LookupLists").Range("A20").Value Then
Recipient = Sheets("LookupLists").Range("B20").Value

But for the code to look up the value in the Named range and then use the row number to reference the cell in the Email Named Range.

Any help would be appreciated, thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try something like this...

Code:
    Dim rngFound As Range
    
    With Sheets("LookupLists")
        Set rngFound = .Range("A:A").Find(Sheets("Sheet2").Range("J7").Value, , xlValues, xlWhole, xlNext, False)
    End With
    
    If Not rngFound Is Nothing Then
        Recipient = rngFound.Offset(, 1)
    Else
        MsgBox ("No match found.")
    End If
 
Upvote 0
Thanks,

it doesn't seem to work though.

It's not recognising a recipient, nor is it generating a msgbox.
 
Upvote 0
Have got it working :)

Instead of using the column reference like ("A:A") I used the named range reference ("Location") which falls in that column.

Thanks for your help
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,702
Members
452,938
Latest member
babeneker

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