Data type error for Color Scale UDF

abc_xyz

New Member
Joined
Jan 12, 2022
Messages
47
Office Version
  1. 2016
Platform
  1. Windows
I have created a VBA UDF to add 3 Color Scale formatting to a given range. The below code seems to be working when the value of Rng is hardcoded as "G5:J5" (with double quotes). Instead, I want it to use just G5:J5 so that I could drag it down.
Can anyone please help?
VBA Code:
Function Macro1(Rng As String)

    Dim MyRng As Range
    Set MyRng = Range(Rng)
 
 
    MyRng.FormatConditions.AddColorScale ColorScaleType:=3
    MyRng.FormatConditions(MyRng.FormatConditions.Count).SetFirstPriority
    
    With MyRng.FormatConditions(1)
    
        With .ColorScaleCriteria(1)
            .Type = xlConditionValueLowestValue
            .FormatColor.Color = 7039480
        End With
        
        With .ColorScaleCriteria(2)
            .Type = xlConditionValuePercentile
            .Value = 50
            .FormatColor.Color = 8711167
        End With
        
        With .ColorScaleCriteria(3)
            .Type = xlConditionValueHighestValue
            .FormatColor.Color = 8109667
        End With
        
    End With
    
End Function
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
The function is expecting a single range, not multiple ranges.
 
Upvote 0
As this is a significantly different question, it needs a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,067
Members
449,090
Latest member
fragment

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