Move row based on cell color

sameh_ag

New Member
Joined
Jun 4, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello
I made a macro to move entire row to the end of sheet based on cell color
Macro is working fine but if 2 or more consecutive rows meet the criteria it skips one, this is because if rows 2 &3 meet the criteria, after moving row 2, row 3 becomes 2 and is considered done
Thanks

VBA Code:
Sub Test()
    
    Dim MR          As Excel.Range
    Dim rngCell     As Excel.Range
    Dim rngCount    As Long
    
    Dim iRow        As Long
    Dim ws          As Worksheet
    Set ws = Worksheets("Strip. & Other")
    
    Set MR = Sheets("Strip. & Other").Range("L1:L300")
    
    rngCount = 1
    
    For Each rngCell In MR
        
        If rngCell.DisplayFormat.Interior.Color = RGB(255, 192, 0) Then
            
            rngCell.Select
            ActiveCell.EntireRow.Cut
            iRow = ws.Range("B:B").Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
            
            Range("A" & iRow).Select
            
            Selection.Insert Shift:=xlDown
            
        Else: GoTo 1
            
        End If
1
    Next rngCell
    
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.
Hi sameh_ag,

To do this you need to work through the list inreverse, from the bottom up. Below finds the last row and starts there and works up through hte list (-1 each time). This way the it captures rows with the same colour next to each other.

I have used colmn 'B' to find the last row as this is where you are searching to add the entries after this row.

Also, I've added in an additional step ! This will keep the entries in the order they are found if the sheet was worked through from the top down, example image 1 below. Delete this row if not needed. Example image 2 is with this row deleted.

VBA Code:
Sub Reverse()

Dim LRow As Long
Dim RowCount As Long
Dim iRow As Long

With Sheets("Strip. & Other")
    
    LRow = Range("B" & Rows.Count).End(xlUp).Row '***Column B find last row
    AddRow = LRow + 1
        
        For RowCount = LRow To 2 Step -1
        
            If .Cells(RowCount, 12).DisplayFormat.Interior.Color = RGB(255, 192, 0) Then
            
            Range("A" & RowCount).EntireRow.Cut
            Range("A" & AddRow).Select
            Selection.Insert Shift:=xlDown
            AddRow = AddRow - 1 '*** Additional step - Delete this if not needed
            
            End If
            
        Next RowCount
        
    End With

End Sub

Test Started With...
Rows 7-8 and 12-13 are together.
1717491501004.png


Example image 1...

Entries run in order found as if list was worked from the top.
1717491410472.png

Example image 2...
Entries are in the order found working from the bottom up.
1717491377883.png
 
Upvote 1
works perfectly, thank you so much sxhall
The order option is excellent, was reluctant to ask for it
Thanks again
 
Upvote 0
I know you have the perfect solution from sxhall but just for the heck of it. Without looping.
Change references as required.
Code:
Sub Move_Colored()
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
Application.ScreenUpdating = False
   With ActiveSheet
      If .AutoFilterMode Then .AutoFilterMode = False
         .Range("A1:A" & lr).AutoFilter 1, RGB(255, 0, 0), xlFilterCellColor
         With .Range("A2:A" & lr).SpecialCells(xlVisible)
         .Copy ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1)
         .EntireRow.Delete Shift:=xlUp
      End With
      .AutoFilterMode = False
   End With
Application.ScreenUpdating = True
End Sub

An easy way to get the RGB color of a cell is to:
Select the colored cell - right click - Format Cells - Fill - More Colors - Custom - Set "Color Model" to RGB.
 
Upvote 1
Solution

Forum statistics

Threads
1,223,879
Messages
6,175,150
Members
452,615
Latest member
bogeys2birdies

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