Difference between Sub and Function

Danny54

Active Member
Joined
Jul 3, 2019
Messages
295
Office Version
  1. 365
Platform
  1. Windows
Running into a situation that has me confused. I have a subroutine called DisplayFormatCount1() and converted it to a Function as I want to insert
=DisplayFormatCount2(M5:M9,65535) into various cells.

the value being returned is 0

Do i need to pass the worksheet to the function?


Orginal VBA Subroutine Here

Sub DisplayFormatCount1()
Dim Rng As Range
Dim CountRange As Range
Dim ColorRange As Range
Dim xBackColor As Long
Dim xFontColor As Long
On Error Resume Next

Set CountRange = Range("$M$5:$M$9")
xxx = 65535

For Each Rng In CountRange
qqq = Rng.Value
xxx = Rng.DisplayFormat.Interior.Color
If Rng.DisplayFormat.Interior.Color = 65535 Then
xBackColor = xBackColor + 1
End If
If Rng.DisplayFormat.Font.Color = ColorRange.DisplayFormat.Font.Color Then
xFontColor = xFontColor + 1
End If
Next
MsgBox "BackColor is " & xBackColor & Chr(10) & "FontColor is " & xFontColor


My attempt to create a UDF Function

Function DisplayFormatCount3(CountRange As Range, inXXX) As Long
Dim CelVal As Range
Dim ColorRange As Range
Dim xBackColor As Long
Dim xFontColor As Long
On Error Resume Next

Set CR = CountRange

For Each CelVal In CR
qqq = CelVal.Value
xxx = CelVal.DisplayFormat.Interior.Color
If CelVal.DisplayFormat.Interior.Color = inXXX Then
xBackColor = xBackColor + 1
End If
If CelVal.DisplayFormat.Font.Color = ColorRange.DisplayFormat.Font.Color Then
xFontColor = xFontColor + 1
End If
Next
MsgBox "BackColor is " & xBackColor & Chr(10) & "FontColor is " & xFontColor
End Function
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You cannot use displayformat directly in a function, when it's called from a sheet.
However you can use a workround like
VBA Code:
Function DisplayFormatCount3(CountRange As Range, inXXX As Long) As Long
Dim CelVal As Range
Dim ColorRange As Range
Dim xBackColor As Long
Dim xFontColor As Long


For Each CelVal In CountRange
   If Evaluate("CFColour(" & CelVal.Address & ",""interior"")") = inXXX Then
      xBackColor = xBackColor + 1
   End If
   If Evaluate("cfcolour(" & CelVal.Address & ",""font"")") = inXXX Then
      xFontColor = xFontColor + 1
   End If
Next
MsgBox "BackColor is " & xBackColor & Chr(10) & "FontColor is " & xFontColor
End Function
Function CFColour(Cl As Range, Itm As String) As Double
   If Itm = "interior" Then
      CFColour = Cl.DisplayFormat.Interior.Color
   ElseIf Itm = "font" Then
      CFColour = Cl.DisplayFormat.Font.Color
   End If
End Function
 
Upvote 0
Thank you.

ok, somewhere i'm still lost. The function call gets inserted into the spreadsheet at L4 "=DisplayFormatCount3(L5:L9,65535)" thru the vbs macro

Now, isn't the execution of the "=DisplayFormatCount4(L5:L9,65535)" supposed to return the value that found in xBackColor?
 
Upvote 0
in a nutshell, what im doing is

create a xls sheet that contains a gantt chart using conditional formatting to make the bars. the whole thing is done by date/hour for over 90 days.
now i need to count the number of times yellow was found in the column by row and insert that count in L4, for column L, count in M4 for column M
and so on.

Since the number of columns and rows are always dynamic i figured i could use a function to do the cound and insert it into L4. Then drag it over to the end of row 4
counting all the yellow filled rows.


Again, thanks for your help




1646865724943.png
 
Upvote 0
In that case you can use
VBA Code:
Function DisplayFormatCount3(CountRange As Range, inXXX As Long) As Long
Dim CelVal As Range

For Each CelVal In CountRange
   If Evaluate("CFColour(" & CelVal.Address & ")") = inXXX Then
      DisplayFormatCount3 = DisplayFormatCount3 + 1
   End If
Next
End Function
Function CFColour(Cl As Range) As Double
   CFColour = Cl.DisplayFormat.Interior.Color
End Function
 
Upvote 0
Solution
Thank you

Im getting a popup box "Ambiguous name detected: CFColour" when resolving the "=DisplayFormatCount3(L5:L9,65535)" inserted into L4
 
Upvote 0
In that case remove the original CFColour function I supplied.
 
Upvote 0
When I remove the CFColour function and resolve the "=DisplayFormatCount3(L5:L9,65535)" inserted into L4 it shows #VALUE!

Function DisplayFormatCount3(CountRange As Range, inXXX As Long) As Long
Dim CelVal As Range

For Each CelVal In CountRange
If Evaluate("CFColour(" & CelVal.Address & ")") = inXXX Then
DisplayFormatCount3 = DisplayFormatCount3 + 1
End If
Next
End Function
 
Upvote 0
Do you still have the CFColour function from post#5 in there as well?
 
Upvote 0
Thanks for the response

With the CFColour function in the macro as below I get the "Ambiguous name detected" message

Without the Function I get a "#VALUE!" inserted in L4


Function DisplayFormatCount3(CountRange As Range, inXXX As Long) As Long
Dim CelVal As Range

For Each CelVal In CountRange
If Evaluate("CFColour(" & CelVal.Address & ")") = inXXX Then
DisplayFormatCount3 = DisplayFormatCount3 + 1
End If
Next
End Function
Function CFColour(Cl As Range) As Double
CFColour = Cl.DisplayFormat.Interior.Color
End Function
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top