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?
 
try
=column()

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
Oops. I forgot that this is the VBA forum for a second there.

try:

myCol = ActiveCell.Column

But =Column() is a standard excel function.

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
haha, that is my second try: that works except when I drag the fomular to other cells the activeCell remain the same!
 
Let me explain more detail.
I wrote a function:
Function theColumn()
theColumn = ActiveCell.Column()
End Function
in cell(1,1) I put =theColumn(), it will show 1, fine.
I then drag it from cell(1,1) to cell(1,2), it will still show 1 in cell(1,2) but I want it show 2.
 
Function theColumn(R As Range)
theColumn = R.Column
End Function
in cell(1,1) put =theColumn(A1)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Add
Code:
Application.Volatile True
to the beginning of your function.


Regards,
Mike
 
wait, what is the point of this? What is the difference between using the UDF theColumn
[tab]Function theColumn()
[tab]theColumn = ActiveCell.Column()
[tab]End Function

and using =column() ?

What are you actually aiming to do?

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
What is the difference between using the UDF theColumn() and using =column() ?
One works, the other not ;-)
 
PHV,

Yes. My actual function for testing is
Code:
Function MyCustom() As Integer
   Application.Volatile True
   MyCustom = ActiveCell.Column
End Function


Regards,
Mike
 
Don't works for me (XL2003)
What happens if you press the F9 key with several cells having this formula ?
 
In my real case, in the user defiened function I need to do something based on the column in which the function located.

 
Have you tried my suggestion ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

I tried you code and that works.
I am thinking why we need to tell the function "R As Range"....why we cannot use =column() in user defined function?
 
PHV,

D'oh. You are right, of course. I got sidetracked with the thought of dragging the formula around, but my "solution" does not work; yours does.

Mike
 
Hi PHV,

you say ...
What is the difference between using the UDF theColumn() and using =column() ?
One works, the other not

I use =Column() all the time, and it does work! Are you sure you are using it correctly ( anotherhiggins knows I'm right on this )?

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
Glenn, I just said that =Column() works and that Mike's =theColumn() doesn't !
 
Ah, wrong end of stick!

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top