VBA codeto color cells with conditions

Veni11

New Member
Joined
Oct 20, 2023
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hello everyone

is there a possible way to color a group of cells between to cells with specific "data".
For example in this table with the names. The Data between the dots should be colored in a color and then the next data between the 2 dots.
So the group (peter,sarah,dave) and the group (lionel,cristiano,ronaldinho,zidane) would be highlighted in different colors.

.
Peter
Sarah
Dave
.
Lionel
Cristiano
Ronaldinho
Zidane
.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi! Something like this should work:
VBA Code:
Sub test()
  Dim dot As Long, i As Long
  If Cells(1, "A").Value = "." Then
    dot = 1
  End If
  For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
    If Cells(i, "A").Value = "." Then
      Cells(i, "A").Offset(-1 * ((i - dot) - 1)).Resize((i - dot) - 1).Interior.ColorIndex = i + 1
      dot = i
    End If
  Next
End Sub
 
Upvote 0
This will be safer:
VBA Code:
Sub test()
  Dim dot As Long, i As Long
  If Cells(1, "A").Value = "." Then 'Change 1 here to the row where your first dot appears
    dot = 1 'Change 1 here to the row where your first dot appears
  End If
  Application.ScreenUpdating = False
  For i = dot + 1 To Cells(Rows.Count, "A").End(xlUp).Row
    If Cells(i, "A").Value = "." Then
      Cells(i, "A").Offset(-1 * ((i - dot) - 1)).Resize((i - dot) - 1).Interior.ColorIndex = i + 1
      dot = i
    End If
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,216
Messages
6,123,669
Members
449,114
Latest member
aides

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