Referring to the Cell where a User Defined function is invoked

DanBlum

New Member
Joined
May 14, 2014
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I have a user defined function that I have written. It is designed to be used in a table in the rows that display certain results. Rows 1-4 of the table are header rows which contains constants (dates and values) that apply to the remaining rows of that particular column. Rows 6 thru n have my user defined function and result in a value in that row and column. I am having trouble referencing the constants in the table. In the code segment below, RowNo, is the row number where the function is invoked. It is passed in as a parameter to the function. I tried the code below to pick up the constant that would be in row 3 of the "current" column. But I am getting surprising results. Some time I get the value in row 3 and sometimes row 2. I suspect my problem is that ActiveCell is not necessarily the cell from which the function is invoked....and that is my problem. How do I reference the cell where the function is invoked?

Code:
    ActiveCell.Offset(2 - RowNo, 0).Value

Also, I want to get the standard reference of the cell. For instance, I want something like
CurrentCell.col to return "G" not 7. . .
if the current cell is in column G. (or do I have to do this conversion myself?) CurrentCell.row does not have this problem. I want this so that I no longer need to pass RowNo as a parameter to the function.

Thanks in advance for helping.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Don't you want your UDF to have a range argument for the constant in the header? That range would be Absolute row and relative column e.g.; A$2, so the column changes as you drag-copy the formula.

This is how to get the column letter from a cell reference

ColLetter = Split(Cells(1, 7).Address, "$")(1)
 
Upvote 0
I suspect my problem is that ActiveCell is not necessarily the cell from which the function is invoked....and that is my problem. How do I reference the cell where the function is invoked?
You are correct... the ActiveCell is the cell that is select on the worksheet, not the cell the formula is in. You will want to use Application.Caller... when called from a UDF, it is a reference to the cell the formula is in. So, if you wanted the column letter designation for the cell your UDF is in, you would get it like this (using what AlphaFrog posted as a base)...

ColLetter = Split(Application.Caller.Address, "$")(1)
 
Upvote 0
Thanks. I prefer not to put any arguments in the header...if I can help it. But I had thought about that as a workaround to my problem. I think the Application.Caller approach is exactly what I was looking for. Thanks to both of you.
 
Upvote 0

Forum statistics

Threads
1,216,462
Messages
6,130,781
Members
449,591
Latest member
sharmavishnu413

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