Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 266
Default Get Address of Current Cell

Below is a function that will return the column of a cell. The "CELL"
function will give a column number - I want the column name e.g. "AM". The
function works - sort of. The problem is if I have a cell with "=COL()" I
can't copy that formula to other cells. I think I'm using the wrong Address
(Selection.Address). Any advice is appreciated. Thanks.

Function Col(Optional Column As Integer)

' This Function is used to return the column of a cell - useful for
"INDIRECT" function
' Typical use is =col(CELL("COL",AM1)) - this would return the value "AM"
' Alternatively can be called as =col() which returns the column of the
current cell


Select Case Column

Case Is 0
FC = Chr(Int((Column - 1) / 26) + 64)
SC = Chr(((Column - 1) Mod 26) + 65)
If Column < 27 Then
Col = SC
Else
Col = FC + SC
End If
Case Is = 0
Addr = Selection.Address
First_Dollar = Application.WorksheetFunction.Find("$", Addr, 1)
Second_Dollar = Application.WorksheetFunction.Find("$", Addr,
First_Dollar + 1)

Col = Mid$(Addr, 2, (Second_Dollar - First_Dollar) - 1)
End Select
End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Get Address of Current Cell

Function colid() As String
v = Application.Caller.Address
colid = Split(v, "$")(1)
End Function

This gives the column ID of the cell containing the formula. (part of your
requirement)
--
Gary''s Student - gsnu200778


"dhstein" wrote:

Below is a function that will return the column of a cell. The "CELL"
function will give a column number - I want the column name e.g. "AM". The
function works - sort of. The problem is if I have a cell with "=COL()" I
can't copy that formula to other cells. I think I'm using the wrong Address
(Selection.Address). Any advice is appreciated. Thanks.

Function Col(Optional Column As Integer)

' This Function is used to return the column of a cell - useful for
"INDIRECT" function
' Typical use is =col(CELL("COL",AM1)) - this would return the value "AM"
' Alternatively can be called as =col() which returns the column of the
current cell


Select Case Column

Case Is 0
FC = Chr(Int((Column - 1) / 26) + 64)
SC = Chr(((Column - 1) Mod 26) + 65)
If Column < 27 Then
Col = SC
Else
Col = FC + SC
End If
Case Is = 0
Addr = Selection.Address
First_Dollar = Application.WorksheetFunction.Find("$", Addr, 1)
Second_Dollar = Application.WorksheetFunction.Find("$", Addr,
First_Dollar + 1)

Col = Mid$(Addr, 2, (Second_Dollar - First_Dollar) - 1)
End Select
End Function

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Get Address of Current Cell

Can you make use of this worksheet formula instead?

=SUBSTITUTE(ADDRESS(1,COLUMN(AM2),4),"1","")

You would put in the cell reference in the COLUMN function, or leave it
empty, and it will give you that cell's column ID in letters.

Rick


"dhstein" wrote in message
...
Below is a function that will return the column of a cell. The "CELL"
function will give a column number - I want the column name e.g. "AM".
The
function works - sort of. The problem is if I have a cell with "=COL()" I
can't copy that formula to other cells. I think I'm using the wrong
Address
(Selection.Address). Any advice is appreciated. Thanks.

Function Col(Optional Column As Integer)

' This Function is used to return the column of a cell - useful for
"INDIRECT" function
' Typical use is =col(CELL("COL",AM1)) - this would return the value
"AM"
' Alternatively can be called as =col() which returns the column of the
current cell


Select Case Column

Case Is 0
FC = Chr(Int((Column - 1) / 26) + 64)
SC = Chr(((Column - 1) Mod 26) + 65)
If Column < 27 Then
Col = SC
Else
Col = FC + SC
End If
Case Is = 0
Addr = Selection.Address
First_Dollar = Application.WorksheetFunction.Find("$", Addr, 1)
Second_Dollar = Application.WorksheetFunction.Find("$", Addr,
First_Dollar + 1)

Col = Mid$(Addr, 2, (Second_Dollar - First_Dollar) - 1)
End Select
End Function


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Get Address of Current Cell

I don't know what you're using in the =indirect(), but that has a parm that
allows you to use R1C1 reference style, too.



dhstein wrote:

Below is a function that will return the column of a cell. The "CELL"
function will give a column number - I want the column name e.g. "AM". The
function works - sort of. The problem is if I have a cell with "=COL()" I
can't copy that formula to other cells. I think I'm using the wrong Address
(Selection.Address). Any advice is appreciated. Thanks.

Function Col(Optional Column As Integer)

' This Function is used to return the column of a cell - useful for
"INDIRECT" function
' Typical use is =col(CELL("COL",AM1)) - this would return the value "AM"
' Alternatively can be called as =col() which returns the column of the
current cell

Select Case Column

Case Is 0
FC = Chr(Int((Column - 1) / 26) + 64)
SC = Chr(((Column - 1) Mod 26) + 65)
If Column < 27 Then
Col = SC
Else
Col = FC + SC
End If
Case Is = 0
Addr = Selection.Address
First_Dollar = Application.WorksheetFunction.Find("$", Addr, 1)
Second_Dollar = Application.WorksheetFunction.Find("$", Addr,
First_Dollar + 1)

Col = Mid$(Addr, 2, (Second_Dollar - First_Dollar) - 1)
End Select
End Function


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 266
Default Get Address of Current Cell

Thanks Gary Rick and Dave. By replacing Addr = Selection.Address with
Addr = Application.Caller.Address it works fine. (thanks Gary). Gary -
your code is much more efficient than mine for the Case = 0 option. Dave -
thanks for the info on the INDIRECT function. I sometimes build an address
by concatenating a row and column e.g A1 might equal "BC" and A2 might
equal 14 - so I use =INDIRECT(A1&A2) which gives me the value in cell BC14.
I'm not sure if the other method of INDIRECT will help with that, which is
why I need the function to return "BC" instead of 55.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Get Address of Current Cell

if A1 contains BC and A2 contains 14, then =indirect(a1&a2) will return what's
in BC14

If A1 contains 55 and a2 contains 14, then =indirect("r"&a2&"c"&a1,false) will
return what's in BC14. There is no need to convert 55 to BC.

It's like writing:
=R14C55
when you're using R1C1 reference style.



dhstein wrote:

Thanks Gary Rick and Dave. By replacing Addr = Selection.Address with
Addr = Application.Caller.Address it works fine. (thanks Gary). Gary -
your code is much more efficient than mine for the Case = 0 option. Dave -
thanks for the info on the INDIRECT function. I sometimes build an address
by concatenating a row and column e.g A1 might equal "BC" and A2 might
equal 14 - so I use =INDIRECT(A1&A2) which gives me the value in cell BC14.
I'm not sure if the other method of INDIRECT will help with that, which is
why I need the function to return "BC" instead of 55.


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Get Address of Current Cell

You are very welcome!
--
Gary''s Student - gsnu200778


"dhstein" wrote:

Thanks Gary Rick and Dave. By replacing Addr = Selection.Address with
Addr = Application.Caller.Address it works fine. (thanks Gary). Gary -
your code is much more efficient than mine for the Case = 0 option. Dave -
thanks for the info on the INDIRECT function. I sometimes build an address
by concatenating a row and column e.g A1 might equal "BC" and A2 might
equal 14 - so I use =INDIRECT(A1&A2) which gives me the value in cell BC14.
I'm not sure if the other method of INDIRECT will help with that, which is
why I need the function to return "BC" instead of 55.

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Get Address of Current Cell

Thanks - I'd been puzzled about how to find the cell that called a macro. I
had reluctantly resorted to passing the calling cell as a parameter.

Application.caller is much better.

Ed

"Gary''s Student" wrote:

Function colid() As String
v = Application.Caller.Address
colid = Split(v, "$")(1)
End Function

This gives the column ID of the cell containing the formula. (part of your
requirement)
--
Gary''s Student - gsnu200778


"dhstein" wrote:

Below is a function that will return the column of a cell. The "CELL"
function will give a column number - I want the column name e.g. "AM". The
function works - sort of. The problem is if I have a cell with "=COL()" I
can't copy that formula to other cells. I think I'm using the wrong Address
(Selection.Address). Any advice is appreciated. Thanks.

Function Col(Optional Column As Integer)

' This Function is used to return the column of a cell - useful for
"INDIRECT" function
' Typical use is =col(CELL("COL",AM1)) - this would return the value "AM"
' Alternatively can be called as =col() which returns the column of the
current cell


Select Case Column

Case Is 0
FC = Chr(Int((Column - 1) / 26) + 64)
SC = Chr(((Column - 1) Mod 26) + 65)
If Column < 27 Then
Col = SC
Else
Col = FC + SC
End If
Case Is = 0
Addr = Selection.Address
First_Dollar = Application.WorksheetFunction.Find("$", Addr, 1)
Second_Dollar = Application.WorksheetFunction.Find("$", Addr,
First_Dollar + 1)

Col = Mid$(Addr, 2, (Second_Dollar - First_Dollar) - 1)
End Select
End Function

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Old address information updated to current addressi Charles Excel Worksheet Functions 1 November 11th 09 03:34 PM
use current cell address as beginning of macro mohavv Excel Discussion (Misc queries) 2 April 18th 08 03:52 PM
Send current file to an e-mail address Gabor G Excel Programming 0 January 24th 04 08:31 AM
Send current file to an e-mail address Gabor G Excel Programming 1 January 23rd 04 12:49 PM
How do I programmatically know the current cell's address Keith[_5_] Excel Programming 3 September 9th 03 08:00 PM


All times are GMT +1. The time now is 05:58 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"