Reference to a Range not working for conditional formatting

Malick

New Member
Joined
Jul 10, 2014
Messages
18
Hi,

I am trying to reference a range in a table, to apply conditional formatting through VBA, but unfortunately it is not working. I am getting my column names of the table by running the FOR EACH loop, and saving the results in 'FormulaNum' string variable. I'm trying to use this variable as a RANGE , when i set my range (rRangeToFormat), but it doesn't work. If i hard code the column names of my table, while setting the range, it does work. Below is my code. Thank you for providing help on this.

Code:
Dim RngFormula As Range    
Dim wsFormula As Worksheet
    Dim FormulaNum As String

    Set wsFormula = Worksheets("VB")
    For Each RngFormula In wsFormula.Range("TblSec")
        If Not IsEmpty(RngFormula) Then
        FormulaNum = FormulaNum & "tbl_1[" & RngFormula & "],"
        End If
    Next RngFormula
    FormulaNum = Left(FormulaNum, Len(FormulaNum) - 1)
    'MsgBox FormulaNum
    Set rRangeToFormat = Worksheets("Vocab").Range(FormulaNum)

 With rRangeToFormat
        .FormatConditions.Delete
        .FormatConditions.AddColorScale ColorScaleType:=3
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Code:
 With rRangeToFormat
        .Select
        .FormatConditions.Delete
        .FormatConditions.AddColorScale ColorScaleType:=3
 
Upvote 0
I have provided the complete code below, including the one for conditional formatting. Although I guess the line below is producing 'Nothing' as far as the RANGE is cocerned.
Code:
Set rRangeToFormat = Worksheets("Vocab").Range(FormulaNum)
Thanks for providing any help as I really struggle to sort this out.
Code:
Dim RngFormula As Range    
Dim wsFormula As Worksheet
    Dim FormulaNum As String


    Set wsFormula = Worksheets("VB")
    For Each RngFormula In wsFormula.Range("TblSec")
        If Not IsEmpty(RngFormula) Then
        FormulaNum = FormulaNum & "tbl_1[" & RngFormula & "],"
        End If
    Next RngFormula
    FormulaNum = Left(FormulaNum, Len(FormulaNum) - 1)
    'MsgBox FormulaNum
    Set rRangeToFormat = Worksheets("Vocab").Range(FormulaNum)


 With rRangeToFormat
.Select
        .FormatConditions.Delete
        .FormatConditions.AddColorScale ColorScaleType:=3
    
        .FormatConditions(1).ColorScaleCriteria(1).Type = xlConditionValuePercent
        .FormatConditions(1).ColorScaleCriteria(1).FormatColor.Color = RGB(99, 190, 123)
        .FormatConditions(1).ColorScaleCriteria(1).FormatColor.TintAndShade = 0.25


        .FormatConditions(1).ColorScaleCriteria(2).Type = xlConditionValuePercent
        .FormatConditions(1).ColorScaleCriteria(2).value = 50
        .FormatConditions(1).ColorScaleCriteria(2).FormatColor.Color = RGB(255, 235, 132)
        .FormatConditions(1).ColorScaleCriteria(2).FormatColor.TintAndShade = 0


        .FormatConditions(1).ColorScaleCriteria(3).Type = xlConditionValuePercent
        .FormatConditions(1).ColorScaleCriteria(3).FormatColor.Color = RGB(248, 105, 107)
        .FormatConditions(1).ColorScaleCriteria(3).FormatColor.TintAndShade = -0.25
    End With
 
Upvote 0
To be honest, it's still less than half a story. What does the named range VB refer to and what's in it? What does the table reference refer to and what's in it? What does the code build? And how is that different to the hard coded column references? What does the working hard coded code look like?

What does "it doesn't work" mean? Does it do nothing? Does it apply the CF but to the wrong cells? Does it crash? Lock up?
 
Upvote 0

Forum statistics

Threads
1,203,727
Messages
6,056,970
Members
444,899
Latest member
Excel_Temp

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