Custom Function Question

jbrouse

Active Member
Joined
Apr 28, 2005
Messages
329
Good morning.

I have created a custom function that is used in many columns. What I need the function to do is reference the value in row 1 of whatever column is calling the function. Can anyone tell me how to pull the cell information that is calling the formula?

So right now, in cell D8, I have the formula =FG($A8,$D8), and I want it to also use the value in D1. I could add it as a 3rd variable, but I also want to be able to use the exact same function in cell E8, but have it pull the value from E1.

Is there any way to do this?

Thanks in advance
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
use D$1 as a reference, then the column but not the row will change as you drag the formula across and down.

eg

=FG($A8,$D8,D$1)

You'll have to change your function accordingly.

You could build it into the function but this way's more consistent.
 
Upvote 0
Hi

Application.Caller will return the cell that contains this function. So you can use the following to pull back the first cell in the column:

Code:
Dim r As Range

Set r = Application.Caller.EntireColumn.Cells(1,1)

'do something:

Msgbox "First cell in column calling is " & r.Address
 
Upvote 0
Use ThisCell:

As an example:

Code:
Function FG() As String
    FG = "A" & Application.ThisCell.Row
End Function
 
Upvote 0
I would add it as a 3rd variable in the function, then write it like this in D8

=FG($A8,$D8,D$1)

The D will incriment to E, then F as you fill/copy the formula to the right.
 
Upvote 0
If you use Application.Caller or ThisCell then you will need to make your function volatile, which is inefficient and unnecessary. I would agree you should add a third argument and pass the cell you need.
 
Upvote 0
I would agree you should add a third argument and pass the cell you need.

Me too, but I think that if you write UDF's, you are also aware of absolute and relative cell references... But you're right, sure.
 
Upvote 0
No idea what you mean by that, I'm afraid. The OP clearly does know about relative vs. absolute references judging by the original formula in use...
 
Upvote 0
Sorry, I misread, ignore my previous comment.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,682
Members
452,937
Latest member
Bhg1984

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