Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default creating custom function - need to receive a cell address as a par

Hello --

I need to create a custom function that I will use in a formula, and it
needs to take action based on the contents of another cell on another
worksheet in the same workbook. The idea is to get a different from the
formula based on the color of the cell on the other worksheet. Sort of like
this:

Public Function ComputeCount (<cell reference) as integer

if <cell reference.Interior.Color = vbRed then
'* do the math one way
else
'* do the math another way
end if

end Function

While I'm pretty proficient at VBA, I'm just not seeing how to get the
function pointing to the cell of interest on the worksheet of interest. I'm
used to setting an Worksheet object variable to reference a given worksheet
and a Range object variable to reference a given cell (such as in
batch-oriented processing), but I've never tried this in a function to be
used in a formula.

Any suggestions? Thanks in advance,

Mark
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 415
Default creating custom function - need to receive a cell address as a par

You need to pass the cell of interest as a argument.

Public Function ComputeCount(CellRef As Range) As String
If CellRef.Interior.Color = vbRed Then
ComputeCount = "It's Red"
Else
ComputeCount = "It's not Red"
End If
End Function

And call it with
=ComputeCount(A1)

NickHK

"Mark VII" ...
Hello --

I need to create a custom function that I will use in a formula, and it
needs to take action based on the contents of another cell on another
worksheet in the same workbook. The idea is to get a different from the
formula based on the color of the cell on the other worksheet. Sort of
like
this:

Public Function ComputeCount (<cell reference) as integer

if <cell reference.Interior.Color = vbRed then
'* do the math one way
else
'* do the math another way
end if

end Function

While I'm pretty proficient at VBA, I'm just not seeing how to get the
function pointing to the cell of interest on the worksheet of interest.
I'm
used to setting an Worksheet object variable to reference a given
worksheet
and a Range object variable to reference a given cell (such as in
batch-oriented processing), but I've never tried this in a function to be
used in a formula.

Any suggestions? Thanks in advance,

Mark



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default creating custom function - need to receive a cell address as a par

Something like this perhaps... The range object does not have to be premised
with the sheet it comes from as that is inherant in the range object
itself... Use the formula like this =computecount(Sheet2!A1)

Public Function ComputeCount(ByVal Cell As Range) As Integer
If Cell.Interior.Color = vbRed Then
'* do the math one way
ComputeCount = 1
Else
'* do the math another way
ComputeCount = 2
End If
End Function

It is important to note that changing the colour of a cell does not trigger
a calculation, nor does it dirty the cell to force a recalculation of the
cells pointing at it... You can add application.volatile to recalc the cell
but you will still have to force a calc when the colour is changed. This will
mean that there will be a fair bit of overhead to this function. Just
something to be aware of...
--
HTH...

Jim Thomlinson


"Mark VII" wrote:

Hello --

I need to create a custom function that I will use in a formula, and it
needs to take action based on the contents of another cell on another
worksheet in the same workbook. The idea is to get a different from the
formula based on the color of the cell on the other worksheet. Sort of like
this:

Public Function ComputeCount (<cell reference) as integer

if <cell reference.Interior.Color = vbRed then
'* do the math one way
else
'* do the math another way
end if

end Function

While I'm pretty proficient at VBA, I'm just not seeing how to get the
function pointing to the cell of interest on the worksheet of interest. I'm
used to setting an Worksheet object variable to reference a given worksheet
and a Range object variable to reference a given cell (such as in
batch-oriented processing), but I've never tried this in a function to be
used in a formula.

Any suggestions? Thanks in advance,

Mark

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default creating custom function - need to receive a cell address as a

Jim --

It is important to note that changing the colour of a cell does not trigger
a calculation, nor does it dirty the cell to force a recalculation of the
cells pointing at it...<


That's an interesting point. Is there a way to detect that the cursor has
left a particular cell, or is there a way to capture the cell that was last
edited? Under my set of somewhat arcane circumstances, I can work around the
lack of automatic recalculation when cell color changes if I can monitor a
given area of the worksheet for edits. My thought is that if I can detect
that a particular cell was edited, I can force a calculation in the dependent
area of my workbook.

Thanks,
Mark


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default creating custom function - need to receive a cell address as a

You can add event code that will reclac on selection change. There is no
event generated when a cell is coloured so selection change is about as close
as you will get. This will force a recalc of all dirty cells each time the
cursor is moved. By adding application.volatile to the original code you get
very close to what you were looking for...

'**In a standard code module
Public Function ComputeCount(ByVal Cell As Range) As Integer
Application.Volatile
If Cell.Interior.Color = vbRed Then
'* do the math one way
ComputeCount = 1
Else
'* do the math another way
ComputeCount = 2
End If
End Function

'** In the Thisworkbook module
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
As Range)
Sh.Calculate
End Sub
--
HTH...

Jim Thomlinson


"Mark VII" wrote:

Jim --

It is important to note that changing the colour of a cell does not trigger
a calculation, nor does it dirty the cell to force a recalculation of the
cells pointing at it...<


That's an interesting point. Is there a way to detect that the cursor has
left a particular cell, or is there a way to capture the cell that was last
edited? Under my set of somewhat arcane circumstances, I can work around the
lack of automatic recalculation when cell color changes if I can monitor a
given area of the worksheet for edits. My thought is that if I can detect
that a particular cell was edited, I can force a calculation in the dependent
area of my workbook.

Thanks,
Mark




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default creating custom function - need to receive a cell address as a

Just to add:


to the OP
Since you indicated the dependent area was in a separate sheet, you might be
a bit more discerning:

Private Sub Workbook_SheetSelectionChange( _
ByVal Sh As Object, ByVal Target As Range)
if Sh.Name = "Colors" then
worksheets("UDFs").Calculate
end if
End Sub

the disadvantage is that this assumes the user will make a selection on that
sheet after changing the color of the subject cell. To account for the
fact that that might not happen, you might want to also put in the
worksheets("UDFs").Calculate command in the sheet deactive event for the
"colors" sheet.

--
Regards,
Tom Ogilvy


"Jim Thomlinson" wrote:

You can add event code that will reclac on selection change. There is no
event generated when a cell is coloured so selection change is about as close
as you will get. This will force a recalc of all dirty cells each time the
cursor is moved. By adding application.volatile to the original code you get
very close to what you were looking for...

'**In a standard code module
Public Function ComputeCount(ByVal Cell As Range) As Integer
Application.Volatile
If Cell.Interior.Color = vbRed Then
'* do the math one way
ComputeCount = 1
Else
'* do the math another way
ComputeCount = 2
End If
End Function

'** In the Thisworkbook module
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
As Range)
Sh.Calculate
End Sub
--
HTH...

Jim Thomlinson


"Mark VII" wrote:

Jim --

It is important to note that changing the colour of a cell does not trigger
a calculation, nor does it dirty the cell to force a recalculation of the
cells pointing at it...<


That's an interesting point. Is there a way to detect that the cursor has
left a particular cell, or is there a way to capture the cell that was last
edited? Under my set of somewhat arcane circumstances, I can work around the
lack of automatic recalculation when cell color changes if I can monitor a
given area of the worksheet for edits. My thought is that if I can detect
that a particular cell was edited, I can force a calculation in the dependent
area of my workbook.

Thanks,
Mark


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
ADDRESS Function as cell ref in equation TheHat Excel Discussion (Misc queries) 4 May 8th 08 09:21 PM
Assign the cell address with a function T.Mad Excel Worksheet Functions 5 February 9th 07 03:21 AM
Creating Custom Function - A Little Help Please! Paige Excel Programming 7 August 17th 06 09:57 AM
Creating a Custom Excel Function to Calculate Gini Coefficients [email protected] Excel Worksheet Functions 3 February 21st 06 10:15 PM
Creating Custom Function: TRIMSTDEV Norvin Laudon Excel Programming 1 October 23rd 03 08:36 PM


All times are GMT +1. The time now is 12:17 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"