Delete the row is not marked with color

vinzent

New Member
Joined
Feb 7, 2012
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hello to all
I have this code using in other file to delete the rows if they not have some color in column A, but I tried to use in other file with more columns and is not working; due I am novice on this,
I am not sure what is wrong on this code :(, the difference is only the color is now in column E, not A :(

Thanks for all.

VBA Code:
Sub Deletenotcolors in column A()
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    'Part 1
    Sheets("Report").Select
    'Delete row 1
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
    'Deleted rows not colored
    Dim Cel As Range, Rng As Range, uRng As Range, c As Long, r As Long
    Set Rng = ActiveSheet.UsedRange
    Set uRng = Rng.Offset(Rng.Rows.Count).Resize(1, 1)
  
    For r = 2 To Rng.Rows.Count
        For c = 1 To Rng.Columns.Count
            Set Cel = Rng.Cells(r, c)
            If Not Cel.EntireRow.Interior.ColorIndex = 19 Then
                Set uRng = Union(uRng, Cel)
                Exit For
            End If
        Next c
    Next r
    uRng.EntireRow.Delete
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
This is the line where column E (the 5th column) is addressed
VBA Code:
For c = 5 To Rng.Columns.Count

Below code should do the job
VBA Code:
Sub DeleteNotColorsInColumnE()
   Application.ScreenUpdating = False
   Application.DisplayAlerts = False

   'Part 1
   Sheets("Report").Select
   'Delete row 1
   Rows("1:1").Select
   Selection.Delete Shift:=xlUp
   'Deleted rows not colored
   Dim Cel As Range, Rng As Range, uRng As Range, c As Long, r As Long
   Set Rng = ActiveSheet.UsedRange
   Set uRng = Rng.Offset(Rng.Rows.Count).Resize(1, 1)
  
   For r = 2 To Rng.Rows.Count
      For c = 5 To Rng.Columns.Count
         Set Cel = Rng.Cells(r, c)
         If Not Cel.EntireRow.Interior.ColorIndex = 19 Then
            Set uRng = Union(uRng, Cel)
            Exit For
         End If
      Next c
   Next r
   uRng.EntireRow.Delete
   Application.ScreenUpdating = True
   Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution
This is the line where column E (the 5th column) is addressed
VBA Code:
For c = 5 To Rng.Columns.Count

Below code should do the job
VBA Code:
Sub DeleteNotColorsInColumnE()
   Application.ScreenUpdating = False
   Application.DisplayAlerts = False

   'Part 1
   Sheets("Report").Select
   'Delete row 1
   Rows("1:1").Select
   Selection.Delete Shift:=xlUp
   'Deleted rows not colored
   Dim Cel As Range, Rng As Range, uRng As Range, c As Long, r As Long
   Set Rng = ActiveSheet.UsedRange
   Set uRng = Rng.Offset(Rng.Rows.Count).Resize(1, 1)
 
   For r = 2 To Rng.Rows.Count
      For c = 5 To Rng.Columns.Count
         Set Cel = Rng.Cells(r, c)
         If Not Cel.EntireRow.Interior.ColorIndex = 19 Then
            Set uRng = Union(uRng, Cel)
            Exit For
         End If
      Next c
   Next r
   uRng.EntireRow.Delete
   Application.ScreenUpdating = True
   Application.DisplayAlerts = True
End Sub
Thanks for reply, this is working for me!!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

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