Macro to sum and concatenate by color

brett1966

New Member
Joined
Apr 20, 2018
Messages
13
Hi All,

I am trying to build a "Time table" to assist with programing irrigation controllers as well as a visual display of how the program looks over a 24 hour period in our orchard (there will be 7 of these calendars - 1 for each day to show the full week of irrigation). The Orchard is divided up into irrigation sections. To which I have assigned colors (but I would like these colors to be added to if I want to add more shifts.....I will explain. I am trying to achieve 3 things in creating a Shift Summary.

1. I want to return the start of a Shift which is represented as time
2. Concatenate the sections in a shift
3. Sum the total minutes per shift.
Please note the times and order of shifts and minutes will change depending on irrigation needs

I have found some code to sum the minutes per irrigation per color but I am not able to adapt it to achieve the other 2 outcomes. I am going around in circles trying to get it to work.

Your expertise would be greatly appreciated.

TIA
Brett

irrigation Shift Summary.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1Saturday, 1 January 2022Shift Summary
2Time of Day0:001:002:003:004:005:006:007:008:009:0010:0011:0012:0013:0014:0015:0016:0017:0018:0020:0022:000:004:008:0012:0018:00
3SectionV1V2Q1Q2E1E2V1V2Q1Q2E1E2F1F2HIJV1 V2 Q1 Q2E1 E2V1 V2 Q1 Q2E1 E2 F1 F2H I J
4Minutes per irrigation6060606060606060606060606060606060601206060240120240240240
Sheet1
Cell Formulas
RangeFormula
X4X4=SumColor(X2,B4:V4)
Y4Y4=SumColor(Y2,B4:V4)
Z4Z4=SumColor(Z2,B4:V4)
AA4AA4=SumColor(AA2,B4:V4)
AB4AB4=SumColor(AB2,B4:V4)
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi Brett,
can you post the VBA code you came up with so far? If I understand it correctly you have SumColor, which solves question 3, but are missing functions for the other 2 questions? So you would need 2 extra functions: =ConcatenateColor(cell_with_color,range_to_concat) and =FirstValueColor(cell_with_color,range_to_check) ?
Thanks,
Koen
 
Upvote 0
Hi Brett,
can you post the VBA code you came up with so far? If I understand it correctly you have SumColor, which solves question 3, but are missing functions for the other 2 questions? So you would need 2 extra functions: =ConcatenateColor(cell_with_color,range_to_concat) and =FirstValueColor(cell_with_color,range_to_check) ?
Thanks,
Koen
Hi Koen,

thanks for your response. I have continued to work on this and a solution came to me in the middle of the night.

I found some VBA code that would return the color number of a colored cell (unfortunately I can't find the place where I got it from to credit the person who wrote and posted it. See code,

VBA 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

I then thought that I can use this color number as a reference to calculate (sum) cells and return the values (concatenate) I need of the specific colored cells.
( I hope that makes sense)

irrigation Shift Summary.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
112:00 AM1:00 AM2:00 AM3:00 AM4:00 AM5:00 AM6:00 AM7:00 AM8:00 AM9:00 AM10:00 AM11:00 AM12:00 PM1:00 PM2:00 PM3:00 PM4:00 PM5:00 PM6:00 PM7:00 PM8:00 PM9:00 PM10:00 PM11:00 PM12:00 AMOrder Summary
2Hours of the day0:001:002:003:004:005:006:0010:0013:0016:0020:0020:0020:0020:0020:0020:0020:0020:0020:0020:0020:0020:0020:0020:00Date09090909090909
3Sunday, 9 January 2022V1V2Q1Q2E1E2H-GV1-Q2E1-F2ABCTime06101316  
4Minutes606060606060240180180240Hours6433400
5Litres Per Second222222327716Litres/Sec2327716  
6SectionV1 V2 Q1 Q2 E1 E2H-GV1-Q2E1-F2ABC  
Sheet1
Cell Formulas
RangeFormula
C2:Y2C2=B2+B4/1440
AB2AB2=A3
AC2AC2=A3
AD2AD2=A3
AE2AE2=A3
AF2AF2=A3
AG2AG2=A3
AH2AH2=A3
AB3AB3=IFERROR(INDEX(B2:Y2,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AB2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AC3AC3=IFERROR(INDEX(B2:Y2,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AC2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AD3AD3=IFERROR(INDEX(B2:Y2,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AD2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AE3AE3=IFERROR(INDEX(B2:Y2,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AE2),COLUMN(B2:Y2))),COLUMN(B6:Y6))),"")
AF3AF3=IFERROR(INDEX(B2:Y2,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AF2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AG3AG3=IFERROR(INDEX(B2:Y2,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AG2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AH3AH3=IFERROR(INDEX(B2:Y2,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AH2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AB4AB4=SumColor(AB2,B4:Y4)/60
AC4AC4=SumColor(AC2,B4:Y4)/60
AD4AD4=SumColor(AD2,B4:Y4)/60
AE4AE4=SumColor(AE2,B4:Y4)/60
AF4AF4=SumColor(AF2,B4:Y4)/60
AG4AG4=SumColor(AG2,B4:Y4)/60
AH4AH4=SumColor(AH2,B4:Y4)/60
AB5AB5=IFERROR(INDEX(B5:Y5,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AB2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AC5AC5=IFERROR(INDEX(B5:Y5,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AC2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AD5AD5=IFERROR(INDEX(B5:Y5,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AD2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AE5AE5=IFERROR(INDEX(B5:Y5,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AE2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AF5AF5=IFERROR(INDEX(B5:Y5,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AF2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AG5AG5=IFERROR(INDEX(B5:Y5,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AG2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AH5AH5=IFERROR(INDEX(B5:Y5,MATCH(MIN(IF(GetCellColor(B2:Y2)=GetCellColor(AH2),COLUMN(B2:Y2))),COLUMN(B2:Y2))),"")
AB6AB6=TEXTJOIN(" ",TRUE,IF(GetCellColor(B3:Y3)=GetCellColor(AB2),B3:Y3,""))
AC6AC6=TEXTJOIN(" ",TRUE,IF(GetCellColor(B3:Y3)=GetCellColor(AC2),B3:Y3,""))
AD6AD6=TEXTJOIN(" ",TRUE,IF(GetCellColor(B2:Y2)=GetCellColor(AD2),B3:Y3,""))
AE6AE6=TEXTJOIN(" ",TRUE,IF(GetCellColor(B2:Y2)=GetCellColor(AE2),B3:Y3,""))
AF6AF6=TEXTJOIN(" ",TRUE,IF(GetCellColor(B3:Y3)=GetCellColor(AF2),B3:Y3,""))
AG6AG6=TEXTJOIN(" ",TRUE,IF(GetCellColor(B2:Y2)=GetCellColor(AG2),B3:Y3,""))
AH6AH6=TEXTJOIN(" ",TRUE,IF(GetCellColor(B2:Y2)=GetCellColor(AH2),B3:Y3,""))


The code behind the SumColor function (sourced from Leila Gharani on her youtube channel)

VBA Code:
Function SumColor(MatchColor As Range, sumRange As Range) As Double

    Dim cell As Range
    Dim myColor As Long
    myColor = MatchColor.Cells(1, 1).Interior.Color
    
    For Each cell In sumRange
        If cell.Interior.Color = myColor Then
        SumColor = SumColor + cell.Value
       End If
       Next cell
End Function

This seems to work.

Thank you once again Koen. I appreciate your interest in my enquiry.
 
Upvote 0
Hi Brett,
that looks good! I'd consider learning a bit more VBA, it's a really handy skill to help with (further) automating Excel tasks :). Anyhow, if you're still interested, this would be a way to do it in VBA. As you can probably see, the functions are just a tiny bit different.
Cheers,
Koen


VBA Code:
Function SumByColor(MatchColor As Range, sumRange As Range) As Double
    
    'Source: https://www.youtube.com/watch?v=OrAvtXI8zVw
    Dim cell As Range
    Dim myColor As Long
    myColor = MatchColor.Cells(1, 1).Interior.Color
    SumByColor = 0
    For Each cell In sumRange
        If cell.Interior.Color = myColor Then
            SumByColor = SumByColor + cell.Value
        End If
    Next cell
End Function

Function ConcatByColor(MatchColor As Range, concatRange As Range) As String
    
    'Source: https://www.mrexcel.com/board/threads/macro-to-sum-and-concatenate-by-color.1192274/#post-5816004
    Dim cell As Range
    Dim myColor As Long
    myColor = MatchColor.Cells(1, 1).Interior.Color
    ConcatByColor = ""
    
    For Each cell In concatRange
        If cell.Interior.Color = myColor Then
            'Put the strings together with a space in between
            ConcatByColor = ConcatByColor & " " & cell.Value
        End If
    Next cell
    ConcatByColor = Trim(ConcatByColor)
End Function

Function FirstByColor(MatchColor As Range, dataRange As Range) As String
    
    'Source: https://www.mrexcel.com/board/threads/macro-to-sum-and-concatenate-by-color.1192274/#post-5816004
    Dim cell As Range
    Dim myColor As Long
    myColor = MatchColor.Cells(1, 1).Interior.Color
    FirstByColor = ""
    
    For Each cell In dataRange
        If cell.Interior.Color = myColor Then
            FirstByColor = cell.Value
            'If one is found, exit the loop
            Exit For
        End If
    Next cell
End Function
 
Upvote 0
Solution
Hi Brett,
that looks good! I'd consider learning a bit more VBA, it's a really handy skill to help with (further) automating Excel tasks :). Anyhow, if you're still interested, this would be a way to do it in VBA. As you can probably see, the functions are just a tiny bit different.
Cheers,
Koen


VBA Code:
Function SumByColor(MatchColor As Range, sumRange As Range) As Double
   
    'Source: https://www.youtube.com/watch?v=OrAvtXI8zVw
    Dim cell As Range
    Dim myColor As Long
    myColor = MatchColor.Cells(1, 1).Interior.Color
    SumByColor = 0
    For Each cell In sumRange
        If cell.Interior.Color = myColor Then
            SumByColor = SumByColor + cell.Value
        End If
    Next cell
End Function

Function ConcatByColor(MatchColor As Range, concatRange As Range) As String
   
    'Source: https://www.mrexcel.com/board/threads/macro-to-sum-and-concatenate-by-color.1192274/#post-5816004
    Dim cell As Range
    Dim myColor As Long
    myColor = MatchColor.Cells(1, 1).Interior.Color
    ConcatByColor = ""
   
    For Each cell In concatRange
        If cell.Interior.Color = myColor Then
            'Put the strings together with a space in between
            ConcatByColor = ConcatByColor & " " & cell.Value
        End If
    Next cell
    ConcatByColor = Trim(ConcatByColor)
End Function

Function FirstByColor(MatchColor As Range, dataRange As Range) As String
   
    'Source: https://www.mrexcel.com/board/threads/macro-to-sum-and-concatenate-by-color.1192274/#post-5816004
    Dim cell As Range
    Dim myColor As Long
    myColor = MatchColor.Cells(1, 1).Interior.Color
    FirstByColor = ""
   
    For Each cell In dataRange
        If cell.Interior.Color = myColor Then
            FirstByColor = cell.Value
            'If one is found, exit the loop
            Exit For
        End If
    Next cell
End Function

Thank's Koen, I will use your code on a mockup spreadsheet and give it a go.

Again appreciate your interest and time

Cheers
Brett
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,684
Members
448,977
Latest member
dbonilla0331

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