Vlookup in VBA to another Workbook

BayEnder111

New Member
Joined
Feb 22, 2013
Messages
12
Hi All,

A bit of a strange one here and I may be approaching it completely in the wrong way but....

I'm trying to perform a Vlookup in VBA from my main workbook against another workbook, however this workbooks name changes each week. Therefore I've created some code to look for the word "Open" in a folder path and open the workbook with this in.

Due to this workbook never having a set name I've defined the workbook, sheet and range so I can reference it in the Vlookup.

In my main workbook I've created a loop to go through a range of cells (D2:D2500) and if they aren't blank perform the vlookup. The Cell Offset is used to determine which cell to output the result to.

I'm getting a type mismatch error when I perform the below:



Code:
strPath = "C:\Users\\Desktop\Automator" strFile = Dir(strPath & "\" & "*Open*" & ".xls")   


If Len(strFile) > 0 Then
  Workbooks.Open strPath & "\" & strFile   
  Sheets(2).Select 
  myFileName = ActiveWorkbook.Name
  mySheetName = ActiveSheet.Name
  myRangeName = Range("E3:AP2500")   
  
ThisWorkbook.Activate
Set rng = Range("D2:D2500") 
    For Each Cell In rng
        If Cell.Value <> "" Then 
           Cell.Offset(0, 15).Value = "=VLOOKUP(C2,[" & myFileName & "]" & mySheetName & "!" & myRangeName & ",2,False)"
    End If
    Next
Else
  MsgBox "Error: Open Problem Lifecycle Not Found": Exit Sub 
End If

Any help would be greatly appreciated.

Thanks.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try...

Code:
myRangeName = Range("E3:AP2500")[COLOR=#ff0000].Address[/COLOR]

Hope this helps!
 
Upvote 0
That's fixed the range error however I'm now getting an error on the highlighted line below:


"Run-Time error '1004':
Application-defined or object-define error


Code:
strPath = "C:\Users\\Desktop\Automator" strFile = Dir(strPath & "\" & "*Open*" & ".xls")   




If Len(strFile) > 0 Then
  Workbooks.Open strPath & "\" & strFile   
  Sheets(2).Select 
  myFileName = ActiveWorkbook.Name
  mySheetName = ActiveSheet.Name
  myRangeName = Range("E3:AP2500").Address   
  
ThisWorkbook.Activate
Set rng = Range("D2:D2500") 
    For Each Cell In rng
        If Cell.Value <> "" Then 
           Cell.Offset(0, 15).Value = "=VLOOKUP(C2,[" & myFileName & "]" & mySheetName & "!" & myRangeName & ",2,False)"
    End If
    Next
Else
  MsgBox "Error: Open Problem Lifecycle Not Found": Exit Sub 
End If




After some debugging I think the issue is that when I'm setting "myFileName" it's referencing my main workbook rather than the one I want to lookup from (With the changing file name).


Is there a way to activate the workbook that has the changing file name?


I may be completely down the wrong path but that's my guess!


Any help greatly appreciated.


Thanks.
 
Upvote 0
You appear to be missing some apostrophes in the formula:

"=VLOOKUP(C2,'[" & myFileName & "]" & mySheetName & "'!" & myRangeName & ",2,False)"
 
Upvote 0
is
Code:
  ThisWorkbook.Activate
Supposed to be from the original workbook?
I can see the code thinking ThisWorkbook being the active workbook.
Might be best to set the variables for your original workbook, before you open the other workbook
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

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