Delete entire row based on cell fill color

jamesdean1379

Board Regular
Joined
Jun 11, 2014
Messages
55
Hey everyone trying to get a VBA code that will delete entire row based on cell fill color. Basically what I have is a spreadsheet and certain cells are filled with grey color and i need to delete the entire row.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hey everyone trying to get a VBA code that will delete entire row based on cell fill color. Basically what I have is a spreadsheet and certain cells are filled with grey color and i need to delete the entire row.
How did the cells get their color... manually set or by means of Conditional Formatting?

Also, what is the Color or ColorIndex value of the gray color that is being used?
 
Upvote 0
How did the cells get their color... manually set or by means of Conditional Formatting?

Also, what is the Color or ColorIndex value of the gray color that is being used?

Rick,

I am not sure of the ColorIndex but the RGB is (192, 192, 192) or Gray -25%. They are manually applied
 
Upvote 0
Rick,

I am not sure of the ColorIndex but the RGB is (192, 192, 192) or Gray -25%. They are manually applied
Assuming the cells you want to process are in Column A, give this macro a try...
Code:
Sub DeleteRowsContainingGrayCells()
  Application.FindFormat.Clear
  Application.FindFormat.Interior.Color = RGB(192, 192, 192)
  With Columns("[B][COLOR="#0000FF"]A[/COLOR][/B]")
    .Cells.Replace "*", "#N/A", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.FindFormat.Clear
End Sub
 
Upvote 0
Assuming the cells you want to process are in Column A, give this macro a try...
Code:
Sub DeleteRowsContainingGrayCells()
  Application.FindFormat.Clear
  Application.FindFormat.Interior.Color = RGB(192, 192, 192)
  With Columns("[B][COLOR=#0000FF]A[/COLOR][/B]")
    .Cells.Replace "*", "#N/A", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.FindFormat.Clear
End Sub

Thanks Rick! works perfectly!
 
Upvote 0
How can this code be adjusted to also delete blank cells in Column A....I have a similar task...and the Macro works, deleting rows based on cell color. But it still leaves rows that contain blank cells (no value)

Assuming the cells you want to process are in Column A, give this macro a try...
Code:
Sub DeleteRowsContainingGrayCells()
  Application.FindFormat.Clear
  Application.FindFormat.Interior.Color = RGB(192, 192, 192)
  With Columns("[B][COLOR=#0000ff]A[/COLOR][/B]")
    .Cells.Replace "*", "#N/A", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.FindFormat.Clear
End Sub
 
Upvote 0
How can this code be adjusted to also delete blank cells in Column A....I have a similar task...and the Macro works, deleting rows based on cell color. But it still leaves rows that contain blank cells (no value)
Give this a try (I highlighted the new row in red)...
Code:
Sub DeleteRowsContainingGrayCells()
  Application.FindFormat.Clear
  Application.FindFormat.Interior.Color = RGB(192, 192, 192)
  With Columns("[B][COLOR="#0000FF"]A[/COLOR][/B]")
    .Cells.Replace "*", "#N/A", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    [B][COLOR="#FF0000"].SpecialCells(xlBlanks).EntireRow.Delete[/COLOR][/B]
    On Error GoTo 0
  End With
  Application.FindFormat.Clear
End Sub
 
Upvote 0
That didn't work for me? Maybe another factor? Another idea I have is can you change the macro to look for a fill color or blank cell and delete based on that?

Give this a try (I highlighted the new row in red)...
Code:
Sub DeleteRowsContainingGrayCells()
  Application.FindFormat.Clear
  Application.FindFormat.Interior.Color = RGB(192, 192, 192)
  With Columns("[B][COLOR=#0000FF]A[/COLOR][/B]")
    .Cells.Replace "*", "#N/A", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    [B][COLOR=#FF0000].SpecialCells(xlBlanks).EntireRow.Delete[/COLOR][/B]
    On Error GoTo 0
  End With
  Application.FindFormat.Clear
End Sub
 
Upvote 0
Or possibly another piece of logic that could work in my case is to say 'whenever a cell is Grey or RGB=192,192,192 keep it and instead delete all others. Kind of the opposite of this.
 
Upvote 0
That didn't work for me? Maybe another factor? Another idea I have is can you change the macro to look for a fill color or blank cell and delete based on that?
That is what my code does. Your blank cell... is it truly blank (no spaces or other hidden characters) or is there a formula in it that is displaying "" as its result?


Or possibly another piece of logic that could work in my case is to say 'whenever a cell is Grey or RGB=192,192,192 keep it and instead delete all others. Kind of the opposite of this.
I cannot adopt the code to do the negative like that, I would have to rewrite the entire macro. Lets see if we can solve the problem above first.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

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