VBA: Object Required - Simple Vlookup Code

Seba Robles

Board Regular
Joined
May 16, 2018
Messages
71
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello,
I'm trying to set a variable in my code with a vlookup within a sheet. I get a runtime error 424 "object required" when it gets to the last line in the code below:

VBA Code:
Dim wsFunc As WorksheetFunction
Set wsFunc = Application.WorksheetFunction

Dim ws As Worksheet
Set ws = Sheets("Parameters")

Dim rngLook As Range
Set rngLook = ws.Range("C:E")

Dim GreetName As Range
Dim MailTo As String

    MailTo = Sheets("Email").Cells(6,8)
    Set GreetName = wsFunc.VLookup(MailTo, rngLook, 3, False)

Any help is greatly appreciated!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
1. You Declare Mailto as String But give it Cell Address Not Value. Change it:
VBA Code:
MailTo = Sheets("Email").Cells(6, 8).Value

2. You Declare GreetName as Range, But I think Vlookup Result Not Range. Are You Sure this is Correct Method?
 
Upvote 0
Solution
Awesome, thank you maabadi!

I added the .value to my MailTo variable and changed the GreetName from Range to Variant, it is working now :D

Here's the code for future reference
VBA Code:
Dim wsFunc As WorksheetFunction
Set wsFunc = Application.WorksheetFunction

Dim ws As Worksheet
Set ws = Sheets("Parameters")

Dim rngLook As Range
Set rngLook = ws.Range("C:E")

Dim GreetName As Variant
Dim MailTo As String

    MailTo = Sheets("Email").Cells(6,8)
    GreetName = wsFunc.VLookup(MailTo, rngLook, 3, False)
 
Upvote 0
if you want Set Range for GreetName Not Value use this:
VBA Code:
Dim wsFunc As WorksheetFunction
Set wsFunc = Application.WorksheetFunction

Dim ws As Worksheet
Set ws = Sheets("Parameters")

Dim Lr As Long
Lr = ws.Range("C" & Rows.Count).End(xlUp).Row

Dim GreetName As Range
Dim MailTo As String
Dim M As Long
    MailTo = Sheets("Email").Cells(6, 8).Value
    M = wsFunc.Match(MailTo, ws.Range("C1:C" & Lr), 0)
   Set GreetName = ws.Cells(M, 5)

And if want Value:
VBA Code:
Dim wsFunc As WorksheetFunction
Set wsFunc = Application.WorksheetFunction

Dim ws As Worksheet
Set ws = Sheets("Parameters")

Dim rngLook As Range
Set rngLook = ws.Range("C:E")

Dim GreetName As String
Dim MailTo As String

    MailTo = Sheets("Email").Cells(6, 8).Value
   GreetName = wsFunc.VLookup(MailTo, rngLook, 3, False)
 
Upvote 0

Forum statistics

Threads
1,214,414
Messages
6,119,373
Members
448,888
Latest member
Arle8907

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