Setting a Variable to a Workbook?

Philip1957

Board Regular
Joined
Sep 30, 2014
Messages
182
Office Version
  1. 365
Platform
  1. Windows
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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.
 
Upvote 0
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
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0
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".
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,923
Members
448,533
Latest member
thietbibeboiwasaco

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