Custom Function Referencing another sheet?

mikechambers

Active Member
Joined
Apr 27, 2006
Messages
397
I have the following function which works fine if it is on the sheet it is trying to search on.

Function MYFUN(RowTerm As String, ColumnTerm As String)
Range("A1").Select
RowNum = Cells.Find(What:=RowTerm, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
Range("A1").Select
ColNum = Cells.Find(What:=ColumnTerm, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Column
MYFUN = Cells(RowNum, ColNum).Value
End Function

BUT, I want to use this on another sheet in the same workbook. How can I have the function on one sheet, but do everything else based on the other sheet? In other words, I want to find the RowTerm and ColumnTerm on the other sheet and give me the intersecting value.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I am assuming that the function code resides in a code module (making it available to all worksheets) and not just as code on the sheet object and that you want to search another sheet and not the active sheet.
So have you tried adding a third argument to the function call - that of Sheetname?
 
Upvote 0
How about?
Code:
Function MYFUN(RowTerm As String, ColumnTerm As String, Optional wsName As String)
    If IsMissing(wsName) Then wsName = ActiveSheet.Name
    With Sheets(wsName)
         MYFUN = .Cells(.Cells.Find(What:=RowTerm, After:=ActiveCell, LookIn:=xlFormulas, _
             LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
             MatchCase:=False, SearchFormat:=False).Row, _
             ws.Cells.Find(What:=ColumnTerm, After:=ActiveCell, LookIn:=xlFormulas, _
             LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
             MatchCase:=False, SearchFormat:=False).Column).Value
     End With
End Function
 
Upvote 0
Hi Mike

You can't select ranges on sheets which aren't active - hence your problem. Can you explain exactly what it is doing and preferably give an example using a html maker to show what it is bringing back. The reason I ask is that I'm not convinced you need a UDF for this. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,397
Members
449,081
Latest member
JAMES KECULAH

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