Call User Defined Function to work with ActiveCell.Column

Vanish29

New Member
Joined
Apr 28, 2016
Messages
26
Good Morning
Task:
Color set of cells, and then sum cells in active column, based on background of cell, then output to specific location.

I found a set of functions that allow me to Count and Sum Cells, based on Color.
I have set up the functions and confirmed they work separately.
Unfortunately, I cannot get everything to now play nice in my userform. I have tried a few configurations, but to no avail.

Here is the code
Code:
Dim A1 As Range
Set A1 = Range("A1")
If CheckBox1.Value = True Then
StatTotal = AutoRedist.ComboBox1.Text
Cells.Find(What:=StatTotal, after:=ActiveCell, LookIn:=xlValues, LookAt:= _
        xlWhole, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False _
        , SearchFormat:=True).Activate
RStat = SumByColor(A1, (ActiveCell.Column))
Cells(lastRow, ActiveCell.Column).Offset(1, 0).Value = RStat
ElseIf ...
Here is the code for the functions, just in case
Code:
Function SumByColor(CellColor As Range, rRange As Range)
Dim cSum As Long
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
    If cl.Interior.ColorIndex = ColIndex Then
        cSum = WorksheetFunction.Sum(cl, cSum)
    End If
Next cl
SumByColor = cSum


End Function
Function CountByColor(CellColor As Range, CountRange As Range)
Dim ICol As Integer
Dim TCell As Range
ICol = CellColor.Interior.ColorIndex
For Each TCell In CountRange
If ICol = TCell.Interior.ColorIndex Then
CountByColor = CountByColor + 1
End If
Next TCell
End Function
The issue seems to be in this line.
Code:
RStat = SumByColor(A1, (ActiveCell.Column))
When calling this function in the spreadsheet itself, it would look like this
Code:
=SumByColor(A1,D:D)
All help is appreciated.
Thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
The clue is in what the function is expecting

Function CountByColor(CellColor As Range, CountRange As Range) it is looking for 2 ranges, but your code is supplying one Range and one Integer

Code:
Set A1 = Range("A1")
RStat = SumByColor(A1, (ActiveCell.Column))

ActiveCell.Column returns the column number (an integer, not a range)


Try
Code:
RStat = SumByColor(A1, ActiveCell.EntireColumn)
 
Last edited:
Upvote 0
Ah that would explain it.
That fixed that issue, and now it calls just fine.
The issue now is the SumByColor now consistently returns a 0, even though the function works normally if called directly in the worksheet as outlined above.
However, the CountByColor works both in the UserForm and directly in the worksheet.
Are you able to see other issues with the code that would cause it to not work with the UserForm?
 
Upvote 0
The function works correctly when called within VBA (see below)
- ranges are your issue one way or another
- ensure VBA is told the SHEET for each range by qualifying the range with the sheet name (or variable)

Code:
Function CountByColor(CellColor As Range, CountRange As Range)
    Dim ICol As Integer, TCell As Range
    ICol = CellColor.Interior.ColorIndex
    For Each TCell In CountRange
        If ICol = TCell.Interior.ColorIndex Then CountByColor = CountByColor + 1
    Next TCell
End Function

Sub HowMany()
    Dim Cel As Range, Rng1 As Range, Rng2 As Range, Apples As Worksheet, Oranges As Worksheet
    Set Apples = Sheets("Apples")
    Set Oranges = Sheets("Oranges")
    Set Cel = Apples.Range("A1")
    Set Rng1 = Apples.Range("D2:D2000")
    Set Rng2 = Oranges.Range("D2:D2000")
    Debug.Print CountByColor(Cel, Rng1)
    Debug.Print CountByColor(Cel, Rng2)
End Sub

See images below, where Debug.Print correctly returns 14 and 6 to the Iimmediate Window - and matches what the formula is returning in D1

Excel 2016 (Windows) 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td=bgcolor:#FFFF00][/td][td][/td][td][/td][td]
14​
[/td][td] =CountByColor(A1,D2:D2000)[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td][/td][td][/td][td][/td][td] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td][/td][td][/td][td][/td][td] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
10
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
11
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
12
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
13
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
14
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
15
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
16
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
17
[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Apples[/td][/tr][/table]

Excel 2016 (Windows) 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td] [/td][td] [/td][td] [/td][td]
6​
[/td][td] =CountByColor(Apples!A1,D2:D2000)[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td] [/td][td] [/td][td] [/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td] [/td][td] [/td][td] [/td][td] [/td][td]
1​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td] [/td][td] [/td][td] [/td][td] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td] [/td][td] [/td][td] [/td][td] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td] [/td][td] [/td][td] [/td][td] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td] [/td][td] [/td][td] [/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td] [/td][td] [/td][td] [/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td] [/td][td] [/td][td] [/td][td] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
10
[/td][td] [/td][td] [/td][td] [/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
11
[/td][td] [/td][td] [/td][td] [/td][td] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
12
[/td][td] [/td][td] [/td][td] [/td][td] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
13
[/td][td] [/td][td] [/td][td] [/td][td] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
14
[/td][td] [/td][td] [/td][td] [/td][td] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
15
[/td][td] [/td][td] [/td][td] [/td][td] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
16
[/td][td] [/td][td] [/td][td] [/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
17
[/td][td] [/td][td] [/td][td] [/td][td=bgcolor:#FFFF00] [/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
18
[/td][td] [/td][td] [/td][td] [/td][td][/td][td][/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Oranges[/td][/tr][/table]
 
Upvote 0
I am not certain I understand
Why would the CountByColor function work when it is called with the same parameters (shown below)
Code:
RStat = CountByColor(A1, ActiveCell.EntireColumn)
But the SumByColor function does not?
Code:
RStat = SumByColor(A1, ActiveCell.EntireColumn)

That suggests that it is not a range issue, but maybe in the function itself?
 
Upvote 0
Hey Yongle,
I am confused as to why we would need to specify which sheet, especially when default is Active Sheet. In addition, the ActiveCell should overrule that and designate active sheet as current sheet.
However, as a good student, I did what you suggested, and it is still returning 0
Here is the updated code:
Code:
Dim ws As Worksheet
Set ws = Sheets("Redistribution")
Set A1 = ws.Range("A1")
If CheckBox1.Value = True Then
StatTotal = AutoRedist.ComboBox1.Text
Cells.Find(What:=StatTotal, after:=ActiveCell, LookIn:=xlValues, LookAt:= _
        xlWhole, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False _
        , SearchFormat:=True).Activate
Set StatCol = ws.Range(ActiveCell.EntireColumn, ActiveCell.EntireColumn)
RStat = SumByColor(A1, StatCol)
Cells(lastRow, ActiveCell.Column).Offset(1, 0).Value = RStat...
 
Upvote 0
Why would the CountByColor function work when it is called with the same parameters but the SumByColor function does not?
That suggests that it is not a range issue, but maybe in the function itself?

Making count and sum range an entire column requires VBA to loop through more than a million rows
How many rows are acually in use?
Consider getting VBA to determine the last row when setting the range

Or use something like this which assumes that data never extends below row 10000
Code:
Sub Test()
    Dim rng As Range, A1 As Range
    Set A1 = Range("A1")
    Set rng = ActiveSheet.Range("A1:A[COLOR=#ff0000]10000[/COLOR]").Offset(, ActiveCell.Column - 1)
    MsgBox "Count= " & CountByColor(A1, rng)
    MsgBox "Sum = " & SumByColor(A1, rng)
End Sub
 
Last edited:
Upvote 0
Interesting.
I got the code to work with that offset portion.
Here is the final code
Code:
Set A1 = ws.Range("A1")
Dim CalcCol As Range
If CheckBox1.Value = True Then
StatTotal = AutoRedist.ComboBox1.Text
Cells.Find(What:=StatTotal, after:=ActiveCell, LookIn:=xlValues, LookAt:= _
        xlWhole, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False _
        , SearchFormat:=True).Activate
Set StatCol = Range(ActiveCell.EntireColumn, ActiveCell.EntireColumn)
Set CalcCol = ws.Range("A" & lastRow).Offset(, ActiveCell.Column - 1)
RStat = SumByColor(A1, CalcCol)
I had a lastrow previously defined so I added it here, per your suggestion.
Much thanks for your help.
While it is not important, as the problem has been solved, but why would ActiveCell.EntireColumn work for this?
Based on all the literature, this should have been the optimal range to use.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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