set Color format and Line style to 'active range'?

SURFER349

Board Regular
Joined
Feb 22, 2017
Messages
50
So i've got this code I'm trying to use to conditionally color format a sheet. I'm putting the color codes at the top to be adjusted easily and then referenced later. It looks for a keyword in column B then applies the format to the entire row.
This works all great and all, but it applies it to the entire sheet, like all columns down to row 1M...
  • How can I adjust this to only apply to $A1:AA1000 or the 'active range' or 'current region'?

Code:
sub colorformatting()
'this sub only needs to be used once on a sheet, it adds conditional formatting that stick to the sheetSub ColorFormating()
Dim Bad As String
Dim Good As String
    Bad = "13551615" 'red
    Good = "13561798" 'green
    
'*NOTE* change the column Letter to reflect the 'Status' column.  Most use ColC, but StepMaster uses colB
With Cells
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
      "=($B1=""DELETE"")"
    With .FormatConditions(.FormatConditions.Count)
        .SetFirstPriority
        With .Interior
            .PatternColorIndex = xlAutomatic
            .Color = BAD 'this is the "BAD" color index
            .TintAndShade = 0

        End With
        StopIfTrue = False
    End With


    .FormatConditions.Add Type:=xlExpression, Formula1:= _
      "=($B1=""NEW"")"
    With .FormatConditions(.FormatConditions.Count)
        .SetFirstPriority
        With .Interior
            .PatternColorIndex = xlAutomatic
            .Color = Good 'this is the "GOOD" color index
            .TintAndShade = 0
        End With
        StopIfTrue = False
    End With
End With
End Sub

I'm also trying to add gridlines (All Borders) to these cells. How can I integrate this recorded script into the original?

Code:
Sub lines()

    Range("A1:AD6").Select
    Range("D4").Activate
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How about
Code:
Sub colorformatting()
'this sub only needs to be used once on a sheet, it adds conditional formatting that stick to the sheetSub ColorFormating()
Dim Bad As String
Dim Good As String
Dim stopiftrue As Boolean
    Bad = "13551615" 'red
    Good = "13561798" 'green
    
'*NOTE* change the column Letter to reflect the 'Status' column.  Most use ColC, but StepMaster uses colB
With Cells
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
      "=($B1=""DELETE"")"
    With .FormatConditions(.FormatConditions.Count)
        .SetFirstPriority
        .Borders.LineStyle = xlContinuous
        .Borders.Weight = xlThin
        With .Interior
            .PatternColorIndex = xlAutomatic
            .Color = Bad 'this is the "BAD" color index
            .TintAndShade = 0

        End With
        stopiftrue = False
    End With


    .FormatConditions.Add Type:=xlExpression, Formula1:= _
      "=($B1=""NEW"")"
    With .FormatConditions(.FormatConditions.Count)
        .SetFirstPriority
        .Borders.LineStyle = xlContinuous
        .Borders.Weight = xlThin

        With .Interior
            .PatternColorIndex = xlAutomatic
            .Color = Good 'this is the "GOOD" color index
            .TintAndShade = 0
        End With
        stopiftrue = False
    End With
End With
End Sub
 
Upvote 0
Cool, so this adds the gridlines, but the formatting still seems to be applying to infinite columns, for 1M+ rows. How do I get this to only be on the current region?

like A1 then cntrol-All

How about
Code:
        .Borders.LineStyle = xlContinuous
        .Borders.Weight = xlThin
 
Upvote 0
Change this
Code:
With Cells
to
With ActiveSheet.UsedRange
 
Upvote 0
most excellent! that works.

One last question. So I'm instructing the column to run it on with each section.
How do I get this to be referenced using a variable? Either define this variable in the top of the sub() or pass it through from a call in another sub? I'm not sure the syntax.

this is the specific section: This column can be sometimes B, sometimes C
Code:
With ActiveSheet.UsedRange    .FormatConditions.Add Type:=xlExpression, Formula1:= _
      "=($C1=""DELETE"")"


maybe something like, but its not working:

Code:
sub MainThing
do stuff

call [COLOR=#333333]colorformatting("B")

[/COLOR]end sub

[CODE]sub [COLOR=#333333]colorformatting(ColStatus as String)
[/COLOR]...
With ActiveSheet.UsedRange    .FormatConditions.Add Type:=xlExpression, Formula1:= _
      "=(ColStatus + "1"=""DELETE"")"
...
end sub
[/CODE]
 
Last edited:
Upvote 0
The formula should be
Code:
"=($" & ColStatus & "1=""DELETE"")"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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