Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

column number in a user defined function 1

Status
Not open for further replies.

welldefined

Programmer
Mar 9, 2006
62
GB
Hi,

In a cell, I will use a function I am going to write. How to get the column number of this cell in the function?
 
Hi,
please note that ActiveCell is an active cell, you get the cell where the function is only when editing it. When the formula is recalculated (esp. after Application.Volatile), new cell can be active and used in function. Instead you can use PHV's formula or:

1. Application.Caller.Column
or, in office xp+:
2. Application.ThisCell.Column

combo
 
Hi combo,

Thiscell is not a property in Excel 97 ... when did that come in?

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
Hi Glenn,
as I marked, this property was introduced in excel xp, returns the cell where the calling UDF is, as Range object.

combo
 
My concentration is not so good today, my apologies. ( I tend to skip over the green popup text adverts )

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
Combo's suggestion + Application.Volatile seems to fit the bill nicely. Using the OP's function definition:
Code:
Function TheColumn()
   Application.Volatile
   TheColumn = Application.Caller.Column
End Function
This function returns the correct column number whether it's dragged from cell to cell, a new column is inserted, etc.

Have a star.

Regards,
Mike
 
Hi rmikesmith,

please forgive before I start ( as I have misread/misunderstood 2 posts today ), but can I ask, why aren't you using =COLUMN() cell formula?

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
Hi Glenn,

I didn't entertain the COLUMN() worksheet function because the OP indicated it didn't work for his purposes and seemed to want a UDF, perhaps to do more than what was detailed. I started from the assumption of a UDF that needed to be able to detect the current cell it was in, but I went all flaky there for a bit (for which PHV provided the reality check). Combo's code allows the UDF to do this. I really don't know what the OP needs this function to do and he hasn't posted lately, but he'd be the one to ask.

Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top