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.
 

Some videos you may like

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Derek Brown

Well-known Member
Joined
Dec 26, 2005
Messages
2,390
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?
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
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
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,411
Messages
5,595,985
Members
414,035
Latest member
billbumkins

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
Top