Conditional Formatting for Selected rows and columns

vck2017

New Member
Joined
Jan 13, 2017
Messages
8
I am trying to apply conditional formatting to a range of cells, whereby formatting is only applied to those rows with an "x" mark in a column, and second only for a range of columns (e.g. rg2)

I have tried the following code, but haven't been able to combine the column and row selection.

I would highly appreciate anyone's help!

Code:
Private Sub ShowMain_Click()
Dim cs As ColorScale
StartR1 = 4
StartR = 5
StartC = 7
Application.ScreenUpdating = False

Range(Cells(StartR, StartC), Cells(10000, StartC).End(xlUp)).EntireRow.Hidden = False
Size = Range(Cells(StartR, StartC), Cells(StartR, StartC).End(xlDown)).Rows.Count
Range(Cells(StartR, StartC), Cells(StartR, StartC).End(xlDown)).EntireRow.Hidden = True
        For Each c In Range(Cells(StartR, 2), Cells(StartR + Size - 1, 2))
        If c = "x" Then Rows(c.Row).Hidden = False
    Next c
    Range(Cells(StartR, StartC), Cells(10000, StartC).End(xlUp)).EntireRow.FormatConditions.Delete
    
    SizeC1 = Range(Cells(StartR1, 1), Cells(StartR1, 1).End(xlToRight)).Columns.Count
    SizeC2 = Range(Cells(StartR1, SizeC1), Cells(StartR1, SizeC1).End(xlToRight)).Columns.Count
    
    Set rg2 = Range(Cells(StartR, SizeC1 + 2), Cells(Size - 1, SizeC2 + 2))
    
    'colour scale will have three colours
    Set rgc = Range("For Each c In Range(Cells(StartR, 2), Cells(StartR + Size - 1, 2))
        If c = "x"
        Next c")
        
    Set cs = rgc.FormatConditions.AddColorScale(ColorScaleType:=3)
    With cs
        'First colour is red
        With .ColorScaleCriteria(1)
            .FormatColor.Color = RGB(255, 0, 0)
            .Type = xlConditionValueNumber
            .Value = -0.2
        End With
        'Second colour is yellow
        With .ColorScaleCriteria(2)
            .FormatColor.Color = RGB(255, 230, 153)
            .Type = xlConditionValueNumber
            .Value = 0
        End With
        'Third colour is Green
        With .ColorScaleCriteria(3)
            .FormatColor.Color = RGB(0, 176, 80)
            .Type = xlConditionValueNumber
            .Value = 0.2
        End With
    End With
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,215,692
Messages
6,126,227
Members
449,303
Latest member
grantrob

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