Setting a Variable to a Workbook?

Philip1957

Board Regular
Joined
Sep 30, 2014
Messages
149
Greetings,

I am trying to write a macro that will perform a Vlookup using a named range in one workbook as the Table Array to populate a column in a named range in a second workbook. I can't seem to get past declaring & setting my variables. This is also my first attempt at doing a Vlookup with VBA.

wb1 is the workbook I'm running the macro from. I define this because in other parts of the project I am using relative path references to locate other file (like the two I'm trying to Vlookup)
wb3 is the workbook I need to run the Vlookup in to populate the second column in the named range.
wb4 is the workbook with the named range that I want to use as the Table Array.

Here's what little code I have so far:

VBA Code:
Private Sub IP_Vlook_Master_to_Lookup()
'Lookup the IP address in IP Master, copy to IP Lookup
Dim wb1 As Workbook
    Set wb1 = ThisWorkbook
Dim wb3 As Workbook
    Set wb3 = ("IP Lookup")
Dim wb4 As Workbook
    Set wb4 = ("IP_Master")

    wb3.Activate
    Range.IPL_tbl!(2, 2).Select


End Sub

The activate & select at the end were simply to test out my variables.

The 2 workbooks, IP Lookup & IP_Master, are already open. When I try to set them I get a "Compile Error: Type Mismatch" and I don't understand why.

Any assistance would be greatly appreciated.

~ Phil
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,657
Office Version
  1. 365
Platform
  1. Windows
It needs to be like
VBA Code:
Set wb3 = Workbooks("IP Lookup")
I would also recommend you include the file extension as well as the name.
 

Philip1957

Board Regular
Joined
Sep 30, 2014
Messages
149
Thank you very much. Syntax gets me every time. I didn't think I needed to use Workbooks since I had already declared it as one.

This isn't the first time you've helped me out and I want you to know that I appreciate it.

~ Phil
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,657
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,954
Office Version
  1. 365
Platform
  1. Windows
I didn't think I needed to use Workbooks since I had already declared it as one.
It doesn't quite work that way. You need to expicitly tell it you are want to set it equal to the workbook named "IP Lookup".
Otherwise, you are trying to set it equal to the literal string value "IP Lookup".
 

Watch MrExcel Video

Forum statistics

Threads
1,129,762
Messages
5,638,207
Members
417,014
Latest member
dualwieldbacon

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
Top