Change code to use fill colors

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello all,

I have the code that highlights my rows, however I would like to not use conditional formatting, but instead use fill color. Is that possible?

Code:
Option ExplicitConst MyAreas = "B5:V341"
Dim a, MyCol As Collection, rng As range, x As range
 
' Highlighting with Conditional Formatting
Private Sub Worksheet_SelectionChange(ByVal Target As range)
  Dim i As Long
  If Application.CutCopyMode Then Exit Sub
  If MyCol Is Nothing Then
    ' Setup MyCol only once first time
    Set MyCol = New Collection
    For Each a In Split(MyAreas, ",")
      MyCol.Add range(a)
      ' Clear CF highligtings in each area for the first time
      range(a).FormatConditions.Delete
    Next
  End If
  If Not x Is Nothing Then
    ' Clear the previous CF highlighting
    x.FormatConditions.Delete
  End If
  For Each x In MyCol
    ' Check intersection
    Set rng = Intersect(Target, x)
    If Not rng Is Nothing Then Exit For
  Next
  If Not x Is Nothing Then
    ' Highlight row of MyAreas via CF
    i = ActiveCell.Interior.ColorIndex
    Set x = x.Rows(rng.Row - x.Row + 1)
    With x.FormatConditions.Add(Type:=2, Formula1:=1)
      .Interior.ColorIndex = IIf(i < 0, 4, i + 1)
      .Font.Bold = True
    End With
  End If
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hello all,

I have the code that highlights my rows, however I would like to not use conditional formatting, but instead use fill color. Is that possible?

Code:
Option ExplicitConst MyAreas = "B5:V341"
Dim a, MyCol As Collection, rng As range, x As range
 
' Highlighting with Conditional Formatting
Private Sub Worksheet_SelectionChange(ByVal Target As range)
  Dim i As Long
  If Application.CutCopyMode Then Exit Sub
  If MyCol Is Nothing Then
    ' Setup MyCol only once first time
    Set MyCol = New Collection
    For Each a In Split(MyAreas, ",")
      MyCol.Add range(a)
      ' Clear CF highligtings in each area for the first time
      range(a).FormatConditions.Delete
    Next
  End If
  If Not x Is Nothing Then
    ' Clear the previous CF highlighting
    x.FormatConditions.Delete
  End If
  For Each x In MyCol
    ' Check intersection
    Set rng = Intersect(Target, x)
    If Not rng Is Nothing Then Exit For
  Next
  If Not x Is Nothing Then
    ' Highlight row of MyAreas via CF
    i = ActiveCell.Interior.ColorIndex
    Set x = x.Rows(rng.Row - x.Row + 1)
    With x.FormatConditions.Add(Type:=2, Formula1:=1)
      .Interior.ColorIndex = IIf(i < 0, 4, i + 1)
      .Font.Bold = True
    End With
  End If
End Sub
Hi Andrew,

It is this part you need to change:

Rich (BB code):
  If Not x Is Nothing Then
    ' Highlight row of MyAreas via CF
    i = ActiveCell.Interior.ColorIndex
    Set x = x.Rows(rng.Row - x.Row + 1)
    With x.FormatConditions.Add(Type:=2, Formula1:=1)
      .Interior.ColorIndex = IIf(i < 0, 4, i + 1)
      .Font.Bold = True
    End With
  End If

I suspect you can just take out the whole conditional formatting stuff from this section and replace it with just something "like" this:

Rich (BB code):
With x
.interior.colorindex = "Your ColorIndex Number" ' for whatever colour you want there.  3 Is red, 4 is green, 5 is blue, 6 is yellow, and so on...
.Font.Bold = True
End With
 
Upvote 0
This turned the entire section red then did not remove the color after.
Sorry Andrew, I have only just noticed your reply. I think without seeing the document itself to make the rest of the code make sense, I am not too sure what else to suggest at this time.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,850
Members
449,051
Latest member
excelquestion515

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