Set WB Name & using VLOOKUP

L

Legacy 313594

Guest
Any particular reason why my vlookup code isnt working? I am using 2 workbooks & want to set the open workbook as "PriorBDay" & use that Name as the VlOokup source.

Please help

Code:
Dim dtTestDate As Date
    Dim sStartWB As String
    
    Const sPath As String = "G:\ManagedAccounts\Operations\ReconTool\Unknown and Halted Accounts\Unknown Account Archive\"
    Const dtEarliest = #3/9/2015# '--to stop loop if file not found by earliest valid date.
    dtTestDate = Date
    sStartWB = ActiveWorkbook.Name
    
    While ActiveWorkbook.Name = sStartWB And dtTestDate >= dtEarliest
        On Error Resume Next
        Workbooks.Open sPath & "Unknown Accounts " & Format(dtTestDate - 1, "MM-DD-YY") & ".xlsx"
        dtTestDate = dtTestDate - 1
        On Error GoTo 0
    Wend
    
    If ActiveWorkbook.Name = sStartWB Then MsgBox "Earlier file not found."
    
    Set PriorBDay = ThisWorkbook
    
'Activate Unknown List Workbook
    Windows("Unknown List.xlsx").Activate

'vLOOKUP EACH WORKSHEET
    Worksheets("EV Accounts").Activate
    
'Value Vlookup
    Range("D2").Formula = _
        "=IF(VLOOKUP(C[-1],'[PriorBDay]EV Accounts'!C3:C4,2,FALSE)=0,"""",(VLOOKUP(C[-1],'[PriorBDay]EV Accounts'!C3:C4,2,FALSE)))"
    Range("D2").Select
    Selection.AutoFill Destination:=Range("D2:D99")
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Instead of
Code:
[PriorBDay]
Try
Code:
" & PriorBDay.Name & "
 
Upvote 0
JASONB75... Are you referring to changing the Vlookup formula to
Code:
" & PriorBDay.Name & "
? Could it also be that my code has
Code:
 sStartWB = ActiveWorkbook.Name
    
    While ActiveWorkbook.Name = sStartWB And dtTestDate >= dtEarliest
        On Error Resume Next
        Workbooks.Open sPath & "Unknown Accounts " & Format(dtTestDate - 1, "MM-DD-YY") & ".xlsx"
        dtTestDate = dtTestDate - 1
Set as the active name?
 
Upvote 0
I was referring to the formula, but previously I missed another problem with it, (also a typo on my part probably didn't help) :oops:

Because PriorBDay is referring to an object in vba a formula in excel will not recognise it, you have to convert it to something that excel can use in a formula first.

The other problem that I just noticed is that your formula uses R1C1 references, but you were trying to write it as an A1 formula which will cause the code to fail because the formula is invalid.

Try replacing

Code:
    Range("D2").Formula = _
        "=IF(VLOOKUP(C[-1],'[PriorBDay]EV Accounts'!C3:C4,2,FALSE)=0,"""",(VLOOKUP(C[-1],'[PriorBDay]EV Accounts'!C3:C4,2,FALSE)))"
    Range("D2").Select
    Selection.AutoFill Destination:=Range("D2:D99")

With

Code:
    Range("D2:D99").FormulaR1C1 = _
        "=IF(VLOOKUP(RC[-1],'[" & PriorBDay.Name & "]EV Accounts'!C3:C4,2,FALSE)=0,"""",(VLOOKUP(RC[-1],'[" & PriorBDay.Name & "]EV Accounts'!C3:C4,2,FALSE)))"

This method eliminates the need for the subsequent autofill. Hopefully it should work this time, if it's slow to run then there are other methods that could speed things up a bit.
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,047
Members
449,206
Latest member
Healthydogs

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