Formula Help

marcidee

Board Regular
Joined
May 23, 2016
Messages
196
Office Version
  1. 2019
Can someone help me 'tweak' this formula to only 'sum' if it sees a '3' in column AP? =SumCellsByColor(B27:AL27, $AV$4)

Your help would be very much appreciated

Marc
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hello,

In order to add a condition to your UDF ...

you should post the code ( Alt + F11 ) with your next message ...
 
Upvote 0
Hello,

In order to add a condition to your UDF ...

you should post the code ( Alt + F11 ) with your next message ...


This is the code:

Function GetCellColor(xlRange As Range)
Dim indRow, indColumn As Long
Dim arResults()

Application.Volatile

If xlRange Is Nothing Then
Set xlRange = Application.ThisCell
End If

If xlRange.Count > 1 Then
ReDim arResults(1 To xlRange.Rows.Count, 1 To xlRange.Columns.Count)
For indRow = 1 To xlRange.Rows.Count
For indColumn = 1 To xlRange.Columns.Count
arResults(indRow, indColumn) = xlRange(indRow, indColumn).Interior.Color
Next
Next
GetCellColor = arResults
Else
GetCellColor = xlRange.Interior.Color
End If
End Function

Function GetCellFontColor(xlRange As Range)
Dim indRow, indColumn As Long
Dim arResults()

Application.Volatile

If xlRange Is Nothing Then
Set xlRange = Application.ThisCell
End If

If xlRange.Count > 1 Then
ReDim arResults(1 To xlRange.Rows.Count, 1 To xlRange.Columns.Count)
For indRow = 1 To xlRange.Rows.Count
For indColumn = 1 To xlRange.Columns.Count
arResults(indRow, indColumn) = xlRange(indRow, indColumn).Font.Color
Next
Next
GetCellFontColor = arResults
Else
GetCellFontColor = xlRange.Font.Color
End If

End Function

Function CountCellsByColor(rData As Range, cellRefColor As Range) As Long
Dim indRefColor As Long
Dim cellCurrent As Range
Dim cntRes As Long

Application.Volatile
cntRes = 0
indRefColor = cellRefColor.Cells(1, 1).Interior.Color
For Each cellCurrent In rData
If indRefColor = cellCurrent.Interior.Color Then
cntRes = cntRes + 1
End If
Next cellCurrent

CountCellsByColor = cntRes
End Function

Function SumCellsByColor(rData As Range, cellRefColor As Range)
Dim indRefColor As Long
Dim cellCurrent As Range
Dim sumRes

Application.Volatile
sumRes = 0
indRefColor = cellRefColor.Cells(1, 1).Interior.Color
For Each cellCurrent In rData
If indRefColor = cellCurrent.Interior.Color Then
sumRes = WorksheetFunction.Sum(cellCurrent, sumRes)
End If
Next cellCurrent

SumCellsByColor = sumRes
End Function

Function CountCellsByFontColor(rData As Range, cellRefColor As Range) As Long
Dim indRefColor As Long
Dim cellCurrent As Range
Dim cntRes As Long

Application.Volatile
cntRes = 0
indRefColor = cellRefColor.Cells(1, 1).Font.Color
For Each cellCurrent In rData
If indRefColor = cellCurrent.Font.Color Then
cntRes = cntRes + 1
End If
Next cellCurrent

CountCellsByFontColor = cntRes
End Function

Function SumCellsByFontColor(rData As Range, cellRefColor As Range)
Dim indRefColor As Long
Dim cellCurrent As Range
Dim sumRes

Application.Volatile
sumRes = 0
indRefColor = cellRefColor.Cells(1, 1).Font.Color
For Each cellCurrent In rData
If indRefColor = cellCurrent.Font.Color Then
sumRes = WorksheetFunction.Sum(cellCurrent, sumRes)
End If
Next cellCurrent

SumCellsByFontColor = sumRes
End Function
 
Upvote 0
Hello,

Below is your modified UDF ...

Code:
Function SumCellsByFontColor(rData As Range, cellRefColor As Range, excepR As Range)
' only 'Sum' if there is a '3' in column AP
' Below is example how to use this Formula
' =SumCellsByFontColor(B27:AL27, $AV$4,AP27)
Dim indRefColor As Long
Dim cellCurrent As Range
Dim sumRes


Application.Volatile
sumRes = 0
indRefColor = cellRefColor.Font.Color
  For Each cellCurrent In rData
    ' Condition Added ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If excepR.Value = 3 Then
      If indRefColor = cellCurrent.Font.Color Then
         sumRes = WorksheetFunction.Sum(cellCurrent, sumRes)
      End If
    End If
  Next cellCurrent


SumCellsByFontColor = sumRes
End Function

Hope this will help
 
Upvote 0
Hello,

Below is your modified UDF ...

Code:
Function SumCellsByFontColor(rData As Range, cellRefColor As Range, excepR As Range)
' only 'Sum' if there is a '3' in column AP
' Below is example how to use this Formula
' =SumCellsByFontColor(B27:AL27, $AV$4,AP27)
Dim indRefColor As Long
Dim cellCurrent As Range
Dim sumRes


Application.Volatile
sumRes = 0
indRefColor = cellRefColor.Font.Color
  For Each cellCurrent In rData
    ' Condition Added ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If excepR.Value = 3 Then
      If indRefColor = cellCurrent.Font.Color Then
         sumRes = WorksheetFunction.Sum(cellCurrent, sumRes)
      End If
    End If
  Next cellCurrent


SumCellsByFontColor = sumRes
End Function

Hope this will help


Thank you very much for this - please can you tell me -does this replace all teh script or just pat of the script?
 
Upvote 0
I have replaced the last part of the code with yours - however I am still getting the same results - can you advise?
 
Upvote 0
Thank you very much for this - please can you tell me -does this replace all teh script or just pat of the script?

The complete UDF has to be replaced by this revised version ... i.e the Function SumCellsByFontColor()
 
Upvote 0
Your whole code contains SIX Functions ...!!!

ONLY the very LAST Function needs to be replaced ...

i.e the Function SumCellsByFontColor()
 
Upvote 0
Your whole code contains SIX Functions ...!!!

ONLY the very LAST Function needs to be replaced ...

i.e the Function SumCellsByFontColor()

Thank you for your patience - I have tried that - I do not get any error messages - however it still adding in the coloured cells that have a 1 or 2 in column AP (results are in AQ if that is relevant, coloured cells are B to AL)

formula in AP is =CountCellsByColor(B4:AL4, $AV$4)
formual in AQ is =SumCellsByColor(B4:AL4, $AV$4)
 
Upvote 0

Forum statistics

Threads
1,223,577
Messages
6,173,163
Members
452,503
Latest member
AM74

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