Vlookup with External Sheet in VBA

vikas_newports

Board Regular
Joined
May 26, 2016
Messages
121
Office Version
  1. 365
Platform
  1. Windows
Hello
I hope all of you are doing well
I want to use vlookup from external file = Data and Worksheet = Support
as I already know how to use vlookup but I do not want to use workbook name and Worksheet name in my formula
this is the existing formula. Please help me to Write formula without using Workbook name and Worksheet Name in Formula

Code:
Range("B2").Formula = "=VLOOKUP(A2,'[DATA.xlsm]Support'!$A$2:$B$500,1,0)"
 
It depends. Can you guarantee they will be the only workbooks open?
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Then you can use code like this to get the name of the other workbook:

Code:
Dim wb As Workbook
For Each wb In Application.Workbooks

    If Not wb Is ThisWorkbook And Not wb.Windows(1).Visible = False Then
        Dim otherWorkbookName As String
        
        otherWorkbookName = wb.Name
        
        Exit For
    End If

Next wb

Range("B2").Formula = "=VLOOKUP(A2,'[" & otherworkbookname & "]Support'!$A$2:$B$500,1,0)"
 
Upvote 0
I don't know what that means?
 
Upvote 0
Yes, as long as the other workbook has a sheet called Support.
 
Upvote 0
I am strange that it is stucking without running
Screenshot_1.jpg
 
Upvote 0
What is the error, and what is the value of otherworkbookname when the error occurs?
 
Upvote 0
Hello Vikas, follow me.
You are lucky man, it is possible to pull data from file with changed names.
To do that your active workbook need to know that new workbook is opened.
After that to create the difference between two workbook.
At the end to add the formula or to do any kind of manipulation between workbooks.
It's practice to create new class and after that to create "Public WithEvents" .
Don't be afraid from class module, in your case is very useful.
Create new module class and name it in the properties of the class "appEventClass".
Put this code in class code editor.
VBA Code:
Option Explicit

Public WithEvents varApp As Application

Private Sub varApp_WorkbookOpen(ByVal Wb As Workbook)
   
    Dim varWB1, varWB2 As Workbook
    If Workbooks.Count = 2 Then
        Set varWB1 = Workbooks(1)
        Set varWB2 = Workbooks(2)
        Application.DisplayAlerts = False
        varWB1.Sheets(1).Range("A1").Formula = "=VLOOKUP(A2,[DATA.xlsm]Support!$A$2:$B$500,1,0)"
        Application.DisplayAlerts = True
    End If
   
End Sub

Now you need to initialize class.
Put this code in the "ThisWorkbook" window.
VBA Code:
Dim varApplicationClass As New appEventClass

Private Sub Workbook_Open()
   
    Set varApplicationClass.varApp = Application
   
End Sub
That's it.
Note this...
Always first open workbook with class "appEventClass".
If you open workbook with data first, the code may replace workbooks places and do not work as you expect.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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