Macro to select cells between "total" rows for gradient fill

CiaoKarina

New Member
Joined
Oct 22, 2021
Messages
12
Office Version
  1. 365
Platform
  1. MacOS
Hello, I have a spreadsheet that shows sales for multiple stores. Column A shows the store name. Within each store, employees are list with their individual sales. I am trying to create a macro to rank the employees WITHIN their store using gradient fill. These cells are not static as the number of employees for each store can change so I want to select the cells in columns F, G and I between the Total rows. See example attached. Thanks in advance for any assistance!
 

Attachments

  • Gradient Fill Example.PNG
    Gradient Fill Example.PNG
    31.1 KB · Views: 7

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi CiaoKarina,

I have some problems understanding how some of the figures are calculated:

MrE_161360A 1221249 _221106_091829.xlsm
ABCDEFGHI
1STOREADV-NAMESALESGROSS# SOLDAVG PRICEAVG GROSS# COD ROS% SOLD
2HONDAJOE1.000,00500,006166,6783,331274,72%
3HONDAMARY2.500,00400,005500,0080,001254,00%
4HONDATotal3.500,00900,0011318,1881,82252???
5displayed2624,20%
6
7TOYOTABILL1.200,00400,0012100,0033,331329,09%
8TOYOTAMARK3.000,00800,0013230,7761,541578,28%
9TOYOTATOM200,0050,001200,0050,00333,03%
10TOYOTAMICHELLE4.500,001.500,0024187,5062,5013517,78%
11TOYOTATotal8.900,002.750,0050178,0055,00457???
12displayed46110,85%
13
14FORDALEX2.500,00500,0014178,5735,711578,92%
15FORDASHLEY3.000,00800,0019157,8942,112597,34%
16FORDSCOTT2.200,00700,0015146,6746,672276,61%
17FORDARTHUR1.200,00800,007171,43114,291305,38%
18FORDTotal8.900,002.800,0055161,8250,91773???
19displayed11676,7224,1414128,22%
20Grand Total21.300,006.450,00116183,6255,601482
CiaoKarina
Cell Formulas
RangeFormula
F20,F14:F18,F7:F11,F2:F4F2=C2/E2
G20,G14:G18,G7:G11,G2:G4G2=D2/E2
H4,C4:E4C4=SUM(C2:C3)
H18,C18:E18,H11,C11:E11C11=SUM(C7:C10)
H20,C20:E20C20=C4+C11+C18


Maybe give this approach a try:

VBA Code:
Sub MrE161360A()
'https://www.mrexcel.com/board/threads/macro-to-select-cells-between-total-rows-for-gradient-fill.1221249/

Dim lngCounter      As Long               'looping through the cells
Dim lngEnd          As Long               'row to start with
Dim lngStart        As Long               'row to end with
Dim lngFG           As Long               'looping through array
Dim varArr          As Variant            'array holding the offset for columns to work on
Dim strAddress      As String             'holding the rather long code to determine where to work

Const cstrVALUE     As String = "Total"   'searching for the exact match
Const cstrCSEARCH   As String = "B"       'Column letter for search

varArr = Array(4, 5, 7)

With ActiveSheet
  For lngCounter = .Cells(.Rows.Count, cstrCSEARCH).End(xlUp).Row To 2 Step -1
    If .Cells(lngCounter, cstrCSEARCH).Value = cstrVALUE Then
      lngEnd = lngCounter - 1
      lngStart = WorksheetFunction.Max(.Cells(lngCounter, cstrCSEARCH).End(xlUp).Row, 2)
      For lngFG = LBound(varArr) To UBound(varArr)
        strAddress = .Range(.Cells(lngStart, cstrCSEARCH), .Cells(lngEnd, cstrCSEARCH)).Offset(, varArr(lngFG)).Address(0, 0)
        If Not FillGradient(strAddress) Then
          MsgBox "Problem occurred filling gradient to range: " & strAddress, vbInformation, "Error here"
        End If
      Next lngFG
      lngCounter = lngStart + 1
    End If
  Next lngCounter
End With

End Sub

Function FillGradient(strRange As String) As Boolean
  
  FillGradient = False
  On Error GoTo end_here
  
  With Range(strRange)
    .FormatConditions.Delete
    .FormatConditions.AddColorScale ColorScaleType:=3
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
  End With
  With Range(strRange).FormatConditions(1)
    With .ColorScaleCriteria(1)
      .Type = xlConditionValueLowestValue
      With .FormatColor
        .Color = 7039480
        .TintAndShade = 0
      End With
    End With
    With .ColorScaleCriteria(2)
      .Type = xlConditionValuePercentile
      .Value = 50
      With .FormatColor
        .Color = 8711167
        .TintAndShade = 0
      End With
    End With
    With .ColorScaleCriteria(3)
      .Type = xlConditionValueHighestValue
      With .FormatColor
        .Color = 8109667
        .TintAndShade = 0
      End With
    End With
  End With
  FillGradient = True
  Exit Function

end_here:

End Function

MrE_161360A 1221249 _221106_091829.xlsm
ABCDEFGHI
1STOREADV-NAMESALESGROSS# SOLDAVG PRICEAVG GROSS# COD ROS% SOLD
2HONDAJOE1.000,00500,006166,6783,331274,72%
3HONDAMARY2.500,00400,005500,0080,001254,00%
4HONDATotal3.500,00900,0011318,1881,82252???
5
6TOYOTABILL1.200,00400,0012100,0033,331329,09%
7TOYOTAMARK3.000,00800,0013230,7761,541578,28%
8TOYOTATOM200,0050,001200,0050,00333,03%
9TOYOTAMICHELLE4.500,001.500,0024187,5062,5013517,78%
10TOYOTATotal8.900,002.750,0050178,0055,00457???
11
12FORDALEX2.500,00500,0014178,5735,711578,92%
13FORDASHLEY3.000,00800,0019157,8942,112597,34%
14FORDSCOTT2.200,00700,0015146,6746,672276,61%
15FORDARTHUR1.200,00800,007171,43114,291305,38%
16FORDTotal8.900,002.800,0055161,8250,91773???
MrE161360A_221106_103024
Cell Formulas
RangeFormula
F12:F16,F6:F10,F2:F4F2=C2/E2
G12:G16,G6:G10,G2:G4G2=D2/E2
H4,C4:E4C4=SUM(C2:C3)
H16,C16:E16,H10,C10:E10C10=SUM(C6:C9)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
I2:I3Other TypeColor scaleNO
G2:G3Other TypeColor scaleNO
F2:F3Other TypeColor scaleNO
I6:I9Other TypeColor scaleNO
G6:G9Other TypeColor scaleNO
F6:F9Other TypeColor scaleNO
I12:I15Other TypeColor scaleNO
G12:G15Other TypeColor scaleNO
F12:F15Other TypeColor scaleNO


Holger
 
Upvote 0
Works like a charm! Thank you so m
Hi CiaoKarina,

I have some problems understanding how some of the figures are calculated:

MrE_161360A 1221249 _221106_091829.xlsm
ABCDEFGHI
1STOREADV-NAMESALESGROSS# SOLDAVG PRICEAVG GROSS# COD ROS% SOLD
2HONDAJOE1.000,00500,006166,6783,331274,72%
3HONDAMARY2.500,00400,005500,0080,001254,00%
4HONDATotal3.500,00900,0011318,1881,82252???
5displayed2624,20%
6
7TOYOTABILL1.200,00400,0012100,0033,331329,09%
8TOYOTAMARK3.000,00800,0013230,7761,541578,28%
9TOYOTATOM200,0050,001200,0050,00333,03%
10TOYOTAMICHELLE4.500,001.500,0024187,5062,5013517,78%
11TOYOTATotal8.900,002.750,0050178,0055,00457???
12displayed46110,85%
13
14FORDALEX2.500,00500,0014178,5735,711578,92%
15FORDASHLEY3.000,00800,0019157,8942,112597,34%
16FORDSCOTT2.200,00700,0015146,6746,672276,61%
17FORDARTHUR1.200,00800,007171,43114,291305,38%
18FORDTotal8.900,002.800,0055161,8250,91773???
19displayed11676,7224,1414128,22%
20Grand Total21.300,006.450,00116183,6255,601482
CiaoKarina
Cell Formulas
RangeFormula
F20,F14:F18,F7:F11,F2:F4F2=C2/E2
G20,G14:G18,G7:G11,G2:G4G2=D2/E2
H4,C4:E4C4=SUM(C2:C3)
H18,C18:E18,H11,C11:E11C11=SUM(C7:C10)
H20,C20:E20C20=C4+C11+C18


Maybe give this approach a try:

VBA Code:
Sub MrE161360A()
'https://www.mrexcel.com/board/threads/macro-to-select-cells-between-total-rows-for-gradient-fill.1221249/

Dim lngCounter      As Long               'looping through the cells
Dim lngEnd          As Long               'row to start with
Dim lngStart        As Long               'row to end with
Dim lngFG           As Long               'looping through array
Dim varArr          As Variant            'array holding the offset for columns to work on
Dim strAddress      As String             'holding the rather long code to determine where to work

Const cstrVALUE     As String = "Total"   'searching for the exact match
Const cstrCSEARCH   As String = "B"       'Column letter for search

varArr = Array(4, 5, 7)

With ActiveSheet
  For lngCounter = .Cells(.Rows.Count, cstrCSEARCH).End(xlUp).Row To 2 Step -1
    If .Cells(lngCounter, cstrCSEARCH).Value = cstrVALUE Then
      lngEnd = lngCounter - 1
      lngStart = WorksheetFunction.Max(.Cells(lngCounter, cstrCSEARCH).End(xlUp).Row, 2)
      For lngFG = LBound(varArr) To UBound(varArr)
        strAddress = .Range(.Cells(lngStart, cstrCSEARCH), .Cells(lngEnd, cstrCSEARCH)).Offset(, varArr(lngFG)).Address(0, 0)
        If Not FillGradient(strAddress) Then
          MsgBox "Problem occurred filling gradient to range: " & strAddress, vbInformation, "Error here"
        End If
      Next lngFG
      lngCounter = lngStart + 1
    End If
  Next lngCounter
End With

End Sub

Function FillGradient(strRange As String) As Boolean
 
  FillGradient = False
  On Error GoTo end_here
 
  With Range(strRange)
    .FormatConditions.Delete
    .FormatConditions.AddColorScale ColorScaleType:=3
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
  End With
  With Range(strRange).FormatConditions(1)
    With .ColorScaleCriteria(1)
      .Type = xlConditionValueLowestValue
      With .FormatColor
        .Color = 7039480
        .TintAndShade = 0
      End With
    End With
    With .ColorScaleCriteria(2)
      .Type = xlConditionValuePercentile
      .Value = 50
      With .FormatColor
        .Color = 8711167
        .TintAndShade = 0
      End With
    End With
    With .ColorScaleCriteria(3)
      .Type = xlConditionValueHighestValue
      With .FormatColor
        .Color = 8109667
        .TintAndShade = 0
      End With
    End With
  End With
  FillGradient = True
  Exit Function

end_here:

End Function

MrE_161360A 1221249 _221106_091829.xlsm
ABCDEFGHI
1STOREADV-NAMESALESGROSS# SOLDAVG PRICEAVG GROSS# COD ROS% SOLD
2HONDAJOE1.000,00500,006166,6783,331274,72%
3HONDAMARY2.500,00400,005500,0080,001254,00%
4HONDATotal3.500,00900,0011318,1881,82252???
5
6TOYOTABILL1.200,00400,0012100,0033,331329,09%
7TOYOTAMARK3.000,00800,0013230,7761,541578,28%
8TOYOTATOM200,0050,001200,0050,00333,03%
9TOYOTAMICHELLE4.500,001.500,0024187,5062,5013517,78%
10TOYOTATotal8.900,002.750,0050178,0055,00457???
11
12FORDALEX2.500,00500,0014178,5735,711578,92%
13FORDASHLEY3.000,00800,0019157,8942,112597,34%
14FORDSCOTT2.200,00700,0015146,6746,672276,61%
15FORDARTHUR1.200,00800,007171,43114,291305,38%
16FORDTotal8.900,002.800,0055161,8250,91773???
MrE161360A_221106_103024
Cell Formulas
RangeFormula
F12:F16,F6:F10,F2:F4F2=C2/E2
G12:G16,G6:G10,G2:G4G2=D2/E2
H4,C4:E4C4=SUM(C2:C3)
H16,C16:E16,H10,C10:E10C10=SUM(C6:C9)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
I2:I3Other TypeColor scaleNO
G2:G3Other TypeColor scaleNO
F2:F3Other TypeColor scaleNO
I6:I9Other TypeColor scaleNO
G6:G9Other TypeColor scaleNO
F6:F9Other TypeColor scaleNO
I12:I15Other TypeColor scaleNO
G12:G15Other TypeColor scaleNO
F12:F15Other TypeColor scaleNO


Holger
Thank you so much! Works like a charm!
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,617
Members
449,039
Latest member
Mbone Mathonsi

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