PC Review


Reply
Thread Tools Rate Thread

How can a VB function determine the cell within which it is runnin

 
 
=?Utf-8?B?VG9ueSBSb2xsaW5z?=
Guest
Posts: n/a
 
      11th Jul 2007
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 With Quote
 
 
 
 
=?Utf-8?B?Sk1C?=
Guest
Posts: n/a
 
      12th Jul 2007
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 With Quote
 
=?Utf-8?B?VG9ueSBSb2xsaW5z?=
Guest
Posts: n/a
 
      12th Jul 2007
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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function to determine if a cell contains a formula DoubleZ Microsoft Excel Misc 5 25th Mar 2010 06:27 PM
In-cell formula/function to determine the color of a cell? sherifffruitfly Microsoft Excel Discussion 3 16th Nov 2008 07:47 PM
Is there a way to determine which cell the function =min() used? =?Utf-8?B?SnVzdGlu?= Microsoft Excel Worksheet Functions 6 16th Oct 2005 01:14 PM
Function to determine if any cell in a range is contained in a given cell choxio@yahoo.com Microsoft Excel Worksheet Functions 3 7th Feb 2005 04:19 PM
Is there a function to determine whether a cell contains a formul. =?Utf-8?B?Q2hyaXN0byBLcmllbA==?= Microsoft Excel Worksheet Functions 1 20th Nov 2004 06:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:28 PM.