Help clean up this recorder generated code.

logandiana

Board Regular
Joined
Feb 21, 2017
Messages
107
As you know when using the recorder it often adds a bunch of stuff you don't need.
I am pretty good about cleaning up, but don't use VBA for conditional formatting that often and am not used to seeing a lot of this.
For each of these ranges, I am deleting the current formatting for the range, and then adding the formatting back using the new range.
I see some negativebar stuff it there but I'll never have a negative value here so I would think I could remove that stuff altogether, but not certain.

VBA Code:
With ws
.Range("C14:C" & LR - 1).Select
    Selection.FormatConditions.Delete
    Selection.FormatConditions.AddDatabar
    Selection.FormatConditions(Selection.FormatConditions.Count).ShowValue = True
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1)
        .MinPoint.Modify newtype:=xlConditionValueAutomaticMin
        .MaxPoint.Modify newtype:=xlConditionValueAutomaticMax
    End With
    With Selection.FormatConditions(1).BarColor
        .Color = 13012579
    End With
.Range("D14:D" & LR - 1).Select
    Selection.FormatConditions(1).BarFillType = xlDataBarFillGradient
    Selection.FormatConditions(1).Direction = xlContext
    Selection.FormatConditions(1).NegativeBarFormat.ColorType = xlDataBarColor
    Selection.FormatConditions(1).BarBorder.Type = xlDataBarBorderSolid
    Selection.FormatConditions(1).NegativeBarFormat.BorderColorType = xlDataBarColor
    With Selection.FormatConditions(1).BarBorder.Color
        .Color = 13012579
    End With
    Selection.FormatConditions(1).AxisPosition = xlDataBarAxisAutomatic
    Selection.FormatConditions.Delete
    Selection.FormatConditions.AddColorScale ColorScaleType:=3
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).ColorScaleCriteria(1).Type = xlConditionValueNumber
    Selection.FormatConditions(1).ColorScaleCriteria(1).Value = 1
    With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor
        .Color = 7039480
    End With
    Selection.FormatConditions(1).ColorScaleCriteria(2).Type = xlConditionValueNumber
    Selection.FormatConditions(1).ColorScaleCriteria(2).Value = 2
    With Selection.FormatConditions(1).ColorScaleCriteria(2).FormatColor
        .Color = 8711167
    End With
    Selection.FormatConditions(1).ColorScaleCriteria(3).Type = xlConditionValueNumber
    Selection.FormatConditions(1).ColorScaleCriteria(3).Value = 3
    With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
        .Color = 8109667
    End With
End with
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Easy answer is to get rid of the Selects and Selections and add a couple more With/End Withs.
Try the below but be warned I haven't tested it at all ;)
You should be able to remove the NegativeBarFormat but being honest you won't notice any speed difference.

VBA Code:
    With ws
        With .Range("C14:C" & LR - 1)
            .FormatConditions.Delete
            .FormatConditions.AddDatabar
            .FormatConditions(.FormatConditions.Count).ShowValue = True
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
            With .FormatConditions(1)
                .MinPoint.Modify newtype:=xlConditionValueAutomaticMin
                .MaxPoint.Modify newtype:=xlConditionValueAutomaticMax
            End With
            With .FormatConditions(1).BarColor
                .Color = 13012579
            End With
        End With
        With .Range("D14:D" & LR - 1)
            .FormatConditions(1).BarFillType = xlDataBarFillGradient
            .FormatConditions(1).Direction = xlContext
            .FormatConditions(1).NegativeBarFormat.ColorType = xlDataBarColor
            .FormatConditions(1).BarBorder.Type = xlDataBarBorderSolid
            .FormatConditions(1).NegativeBarFormat.BorderColorType = xlDataBarColor
            With .FormatConditions(1).BarBorder.Color
                .Color = 13012579
            End With
            .FormatConditions(1).AxisPosition = xlDataBarAxisAutomatic
            .FormatConditions.Delete
            .FormatConditions.AddColorScale ColorScaleType:=3
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
            .FormatConditions(1).ColorScaleCriteria(1).Type = xlConditionValueNumber
            .FormatConditions(1).ColorScaleCriteria(1).Value = 1
            With .FormatConditions(1).ColorScaleCriteria(1).FormatColor
                .Color = 7039480
            End With
            .FormatConditions(1).ColorScaleCriteria(2).Type = xlConditionValueNumber
            .FormatConditions(1).ColorScaleCriteria(2).Value = 2
            With .FormatConditions(1).ColorScaleCriteria(2).FormatColor
                .Color = 8711167
            End With
            .FormatConditions(1).ColorScaleCriteria(3).Type = xlConditionValueNumber
            .FormatConditions(1).ColorScaleCriteria(3).Value = 3
            With .FormatConditions(1).ColorScaleCriteria(3).FormatColor
                .Color = 8109667
            End With
        End With
    End With
 
Upvote 0
It could also be like that

VBA Code:
      With ws.Range("C14:C" & LR - 1)
            .FormatConditions.Delete
            .FormatConditions.AddDatabar
            .FormatConditions(.FormatConditions.Count).ShowValue = True
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
            With .FormatConditions(1)
                .MinPoint.Modify newtype:=xlConditionValueAutomaticMin
                .MaxPoint.Modify newtype:=xlConditionValueAutomaticMax
            End With
            With .FormatConditions(1).BarColor
                .Color = 13012579
            End With
        End With
'
        With ws.Range("D14:D" & LR - 1)
            .FormatConditions(1).BarFillType = xlDataBarFillGradient
            .FormatConditions(1).Direction = xlContext
            .FormatConditions(1).NegativeBarFormat.ColorType = xlDataBarColor
            .FormatConditions(1).BarBorder.Type = xlDataBarBorderSolid
            .FormatConditions(1).NegativeBarFormat.BorderColorType = xlDataBarColor
            With .FormatConditions(1).BarBorder.Color
                .Color = 13012579
            End With
            .FormatConditions(1).AxisPosition = xlDataBarAxisAutomatic
            .FormatConditions.Delete
            .FormatConditions.AddColorScale ColorScaleType:=3
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
            .FormatConditions(1).ColorScaleCriteria(1).Type = xlConditionValueNumber
            .FormatConditions(1).ColorScaleCriteria(1).Value = 1
            With .FormatConditions(1).ColorScaleCriteria(1).FormatColor
                .Color = 7039480
            End With
            .FormatConditions(1).ColorScaleCriteria(2).Type = xlConditionValueNumber
            .FormatConditions(1).ColorScaleCriteria(2).Value = 2
            With .FormatConditions(1).ColorScaleCriteria(2).FormatColor
                .Color = 8711167
            End With
            .FormatConditions(1).ColorScaleCriteria(3).Type = xlConditionValueNumber
            .FormatConditions(1).ColorScaleCriteria(3).Value = 3
            With .FormatConditions(1).ColorScaleCriteria(3).FormatColor
                .Color = 8109667
            End With
        End With
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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