Refactor VBA for scrolling to the next colored cell (with different color than selected)

thardy00

New Member
Joined
Jan 25, 2022
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
Hi guys.
I've found online a VBA made by p45cal which scrolls the view to the next colored cell in a selected column and it's working fine, but I would like to make some tweaks to it.
  1. Right now it is scrolling to the next colored cell as it should, but I would like to modify it to make it scrolling to the next different color, for e.g. we have a column in which we have:
    C2 - red background fill
    C3 - red background fill
    C4 - yellow background fill
    right now it will jump from C2 to C3. I would like to make it scroll to C4 as its the next different color.
  2. It does not treat No Fill as a color. If it is possible I would like to change that.
Here's the code:
VBA Code:
Sub AnyColour()
Dim StartCellDefined As Boolean
Set ur = ActiveSheet.UsedRange
If Intersect(ur, ActiveCell) Is Nothing Then
  StartCellDefined = True
Else
  rw = ActiveCell.Row - ur.Row + 1
  Colm = ActiveCell.Column - ur.Column + 1
End If
For myRw = 1 To ur.Rows.Count
  For myColm = 1 To ur.Columns.Count
    If Not StartCellDefined Then
      myRw = rw
      myColm = Colm + 1
      StartCellDefined = True
    End If
    If ur.Cells(myRw, myColm).Interior.ColorIndex <> xlNone Then
      ur.Cells(myRw, myColm).Activate
      Exit Sub
    End If
  Next myColm
Next myRw
For myRw = 1 To ur.Rows.Count
  For myColm = 1 To ur.Columns.Count
    If myRw = rw And myColm = Colm Then
      EndReached = True
      Exit For
    End If
    If ur.Cells(myRw, myColm).Interior.ColorIndex <> xlNone Then
      ur.Cells(myRw, myColm).Activate
      Exit Sub
    End If
  Next myColm
  If EndReached Then Exit For
Next myRw
End Sub

If someone will have some time and could take a look at this I will really appreciate the help. I've tried to sort it out myself, but my knowledge of VBA is very limited.

Best regards,
Tom.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
This should work:
VBA Code:
Sub test()
  Dim interiorColor As Variant
  interiorColor = ActiveCell.Interior.Color
  For i = ActiveCell.Row To Rows.Count
    If Cells(i, ActiveCell.Column).Interior.Color <> interiorColor Then
      Cells(i, ActiveCell.Column).Select
      Exit Sub
    End If
  Next
  MsgBox "No different color found!"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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