Creating a UDF that only looks to a specific workbook, not just the active workbook

Lang43

New Member
Joined
Mar 7, 2013
Messages
2
Good Morning Everyone,

I have a bit of a problem. I created a glorified lookup UDF in order to be able to properly look up values from one tab to another within a workbook.

The problem I am having is that there are multiple worbooks(like templates) that have the exact same setup. So if the user has more than one workbook open and the workbook recalculates, both open workbooks look to the Activeworkbook and the inactive workbook pulls the wrong info.

Current Code:


Code:
[CODE][CODE]Function NetPay(Scenario As Range)
   Dim CC As Integer
   CC = Application.Caller.Column
   
    Select Case Scenario
        
        Case 1: NetPay = ActiveWorkbook.Sheets("Scenarios").Cells(4, CC)
        Case 2: NetPay = ActiveWorkbook.Sheets("Scenarios").Cells(7, CC)
        Case 3: NetPay = ActiveWorkbook.Sheets("Scenarios").Cells(10, CC)
        Case 4: NetPay = ActiveWorkbook.Sheets("Scenarios").Cells(13, CC)
        Case 5: NetPay = ActiveWorkbook.Sheets("Scenarios").Cells(16, CC)
        Case 6: NetPay = ActiveWorkbook.Sheets("Scenarios").Cells(19, CC)
        Case 7: NetPay = ActiveWorkbook.Sheets("Scenarios").Cells(22, CC)
        Case 8: NetPay = ActiveWorkbook.Sheets("Scenarios").Cells(25, CC)
        Case 9: NetPay = ActiveWorkbook.Sheets("Scenarios").Cells(28, CC)
        
    End Select
    
    
End Function

I am trying to use a =Cell("filename") variation within the workbook to specify the workbook in a cell, then write code in a UDF that will make each UDF specifically Look that workbooks tab for the information.

Something like this

Code:
Function NetPay(Scenario As Range, Filename As Variant)
   Dim CC As Integer
   CC = Application.Caller.Column
   
    Select Case Scenario
        
        Case 1: NetPay = Filename.Sheets("Scenarios").Cells(4, CC)
        Case 2: NetPay = Filename.Sheets("Scenarios").Cells(7, CC)
        Case 3: NetPay = Filename.Sheets("Scenarios").Cells(10, CC)
        Case 4: NetPay = Filename.Sheets("Scenarios").Cells(13, CC)
        Case 5: NetPay = Filename.Sheets("Scenarios").Cells(16, CC)
        Case 6: NetPay = Filename.Sheets("Scenarios").Cells(19, CC)
        Case 7: NetPay = Filename.Sheets("Scenarios").Cells(22, CC)
        Case 8: NetPay = Filename.Sheets("Scenarios").Cells(25, CC)
        Case 9: NetPay = Filename.Sheets("Scenarios").Cells(28, CC)
        
    End Select
    
    
End Function

So, as you can see, I have drawn a blank as to how to make this UDF only look to the file it is in and then look up the value, instead of just looking up the active workbooks tab.

This is more of a neccesity for data integrity.

ANyone happen to have an idea on how to make this work better?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi

If I understand correctly the udf should refer to the workbook where the cell that uses the udf is located.

You can start by getting the workbook

Code:
Dim wb As Workbook

Set wb = Application.Caller.Parent.Parent

and then use wb to qualify the sheets:

Code:
        Case 5: NetPay = wb.Sheets("Scenarios").Cells(16, CC)
 
Upvote 0
Excellent! I keep forgetting those easy codes. Still learning and I appreciate your help!
 
Upvote 0

Forum statistics

Threads
1,215,890
Messages
6,127,595
Members
449,386
Latest member
owais87

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