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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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