Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How can a VB function determine the cell within which it is runnin

I currently have a VB function that takes a string argument and a column
number argument. It scans a key column for the string (using the MATCH
worksheet function) and returns the the value at column number in the matched
row:
=kval("XXXXX", COLUMN(E1))
for example, to get the cell value from column E of the row that contains
"XXXXX" in the key column.

Frequently, I use the function to return a value from the same column that
the function reference is in:
=kval("XXXXX", COLUMN())

I would like to be able to compute the value that COLUMN() (or ROW()) would
supply within the function itself. How can I do this?
  #2   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default How can a VB function determine the cell within which it is runnin

Try Application.Caller. For example:

Function kval(strTest As String, Optional lngCol As Long = 0) As Long
With Application
.Volatile True
If lngCol = 0 And TypeName(.Caller) = "Range" Then
lngCol = .Caller.Column
End If
End With

kval = lngCol

End Function


Perhaps you've simplified your explanation for our benefit, but have you
considered using built in lookup functions?

Since you are using match within your UDF, I would think you should be able
to make it work w/INDEX. For example:
=INDEX(DataCol, MATCH("XXXXX", KeyCol, 0))



"Tony Rollins" wrote:

I currently have a VB function that takes a string argument and a column
number argument. It scans a key column for the string (using the MATCH
worksheet function) and returns the the value at column number in the matched
row:
=kval("XXXXX", COLUMN(E1))
for example, to get the cell value from column E of the row that contains
"XXXXX" in the key column.

Frequently, I use the function to return a value from the same column that
the function reference is in:
=kval("XXXXX", COLUMN())

I would like to be able to compute the value that COLUMN() (or ROW()) would
supply within the function itself. How can I do this?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How can a VB function determine the cell within which it is ru

Thank you very much, this is exactly what I'm looking for.

To your question, my function is intended to be a wrapper for built in
functions so I can simplify cell expression syntax. The application involves
several hundred such references, so any content reduction I can make
simplifies life a lot.

"JMB" wrote:

Try Application.Caller. For example:

Function kval(strTest As String, Optional lngCol As Long = 0) As Long
With Application
.Volatile True
If lngCol = 0 And TypeName(.Caller) = "Range" Then
lngCol = .Caller.Column
End If
End With

kval = lngCol

End Function


Perhaps you've simplified your explanation for our benefit, but have you
considered using built in lookup functions?

Since you are using match within your UDF, I would think you should be able
to make it work w/INDEX. For example:
=INDEX(DataCol, MATCH("XXXXX", KeyCol, 0))



"Tony Rollins" wrote:

I currently have a VB function that takes a string argument and a column
number argument. It scans a key column for the string (using the MATCH
worksheet function) and returns the the value at column number in the matched
row:
=kval("XXXXX", COLUMN(E1))
for example, to get the cell value from column E of the row that contains
"XXXXX" in the key column.

Frequently, I use the function to return a value from the same column that
the function reference is in:
=kval("XXXXX", COLUMN())

I would like to be able to compute the value that COLUMN() (or ROW()) would
supply within the function itself. How can I do this?

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
Function to determine if a cell contains a formula DoubleZ Excel Discussion (Misc queries) 5 March 25th 10 06:27 PM
Function to determine whether cell contains a formula? Don Excel Worksheet Functions 4 March 2nd 07 04:11 AM
Value of cell to determine range in MAX Function [email protected] Excel Worksheet Functions 8 February 18th 06 06:34 PM
Is there a way to determine which cell the function =min() used? Justin Excel Worksheet Functions 6 October 16th 05 01:14 PM
Is there a function to determine whether a cell contains a formul. Christo Kriel Excel Worksheet Functions 1 November 20th 04 06:54 PM


All times are GMT +1. The time now is 02:39 AM.

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"