View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
NickHK[_3_] NickHK[_3_] is offline
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