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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
How about
VBA Code:
Function Macro1(Rng As Range)


 
    Rng.FormatConditions.AddColorScale ColorScaleType:=3
    Rng.FormatConditions(MyRng.FormatConditions.Count).SetFirstPriority
    
    With Rng.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
 
Upvote 0
It works fine. But the cell on which the UDF is written shows #VALUE. Instead, can we add TURE or DONE?
How about
VBA Code:
Function Macro1(Rng As Range)


 
    Rng.FormatConditions.AddColorScale ColorScaleType:=3
    Rng.FormatConditions(MyRng.FormatConditions.Count).SetFirstPriority
  
    With Rng.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
 
Upvote 0
Add this just before the End Sub
Excel Formula:
    Macro1 = "Done"
 
Upvote 0
In that case please post the code you are now using.
 
Upvote 0
Here..
VBA Code:
Function NewMacro(Rng As Range)

    Rng.FormatConditions.Delete
    Rng.FormatConditions.AddColorScale ColorScaleType:=3
    Rng.FormatConditions(MyRng.FormatConditions.Count).SetFirstPriority
    
    With Rng.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
    
    NewMacro = "Done"
    
End Function
 
Upvote 0
I missed changing one of the MyRng variables it should be
VBA Code:
    Rng.FormatConditions(Rng.FormatConditions.Count).SetFirstPriority
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,212
Members
449,090
Latest member
bes000

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