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.
 
Rick, it is blank...I just filtered the column and they are truly blanks...there's no formula on the back end. All other rows get deleted, but they contain some value....only the blanks remain.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Rick, it is blank...I just filtered the column and they are truly blanks...there's no formula on the back end. All other rows get deleted, but they contain some value....only the blanks remain.
Then I am at a loss as to why the code I posted did not work... I just tested it here and it works as expected for me... as long as the cells in Column A are truly blank with nothing in them, the code I posted deleted their rows along with the colored cells' rows. Any chance you can post a copy of your workbook (with its original data from before you tried my code) to a file sharing facility like OneDrive, DropBox, etc. so I can watch my code at work "live"?
 
Upvote 0
Sure, below is the link to the file. The column in my case is Column E, not A. And the color that I color I'm filtering for is Grey (192,192,192) and getting rid of all Yellow (255,255,153), which is where the blanks are.

https://app.box.com/s/s7tfpva3w4v7hgnqmfmzrqg0p4w545ln

Then I am at a loss as to why the code I posted did not work... I just tested it here and it works as expected for me... as long as the cells in Column A are truly blank with nothing in them, the code I posted deleted their rows along with the colored cells' rows. Any chance you can post a copy of your workbook (with its original data from before you tried my code) to a file sharing facility like OneDrive, DropBox, etc. so I can watch my code at work "live"?
 
Upvote 0
Sure, below is the link to the file. The column in my case is Column E, not A. And the color that I color I'm filtering for is Grey (192,192,192) and getting rid of all Yellow (255,255,153), which is where the blanks are.

https://app.box.com/s/s7tfpva3w4v7hgnqmfmzrqg0p4w545ln
Okay, that is interesting... your worksheet is acting like it has something in those blank cells when there appears to be nothing in them. Sometimes when data is copied, cells that originally had "" in them come across without the "", but it is still there (as a null string I think). Anyway, the way to clear the problem is to assign the value of the cell back upon itself. Doing that for your data make my code work correctly. Here is the modified code for you to try...
Code:
[table="width: 500"]
[tr]
	[td]Sub DeleteRowsContainingGrayCells()
  Application.ScreenUpdating = False
  Application.FindFormat.Clear
  Application.FindFormat.Interior.Color = RGB(192, 192, 192)
  With Columns("E")
    With Intersect(.Cells, ActiveSheet.UsedRange)
      .Value = .Value
    End With
    .Cells.Replace "*", "#N/A", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    .SpecialCells(xlBlanks).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.FindFormat.Clear
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Okay, that is interesting... your worksheet is acting like it has something in those blank cells when there appears to be nothing in them. Sometimes when data is copied, cells that originally had "" in them come across without the "", but it is still there (as a null string I think). Anyway, the way to clear the problem is to assign the value of the cell back upon itself. Doing that for your data make my code work correctly. Here is the modified code for you to try...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub DeleteRowsContainingGrayCells()
  Application.ScreenUpdating = False
  Application.FindFormat.Clear
  Application.FindFormat.Interior.Color = RGB(192, 192, 192)
  With Columns("E")
    With Intersect(.Cells, ActiveSheet.UsedRange)
      .Value = .Value
    End With
    .Cells.Replace "*", "#N/A", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    .SpecialCells(xlBlanks).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.FindFormat.Clear
  Application.ScreenUpdating = True
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
Looks like a post I just responded to, but the OP wants to delete everything except the RGB(192,192,192) filled cells. Rick, why not replace the formatted cells content with "" (instead of #N/A) and do a single specialcells call for xlBlanks?
 
Upvote 0
Looks like a post I just responded to, but the OP wants to delete everything except the RGB(192,192,192) filled cells. Rick, why not replace the formatted cells content with "" (instead of #N/A) and do a single specialcells call for xlBlanks?
@JoeMo,

Well... because.... let's see.... I mean.... well, it is like this... uh... I got nothing! :oops:

That is a great idea that I simply did not think of. :oops:



@ldashev,

This is what JoeMo meant...
Code:
[table="width: 500"]
[tr]
	[td]Sub DeleteRowsContainingGrayCells()
  Application.ScreenUpdating = False
  Application.FindFormat.Clear
  Application.FindFormat.Interior.Color = RGB(192, 192, 192)
  With Columns("E")
    With Intersect(.Cells, ActiveSheet.UsedRange)
      .Value = .Value
    End With
    .Cells.Replace "*", "", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    .SpecialCells(xlBlanks).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.FindFormat.Clear
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Haha, Rick, I just tried your code and it worked to perfection!! You're the best!

@JoeMo,

Well... because.... let's see.... I mean.... well, it is like this... uh... I got nothing! :oops:

That is a great idea that I simply did not think of. :oops:



@ldashev,

This is what JoeMo meant...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub DeleteRowsContainingGrayCells()
  Application.ScreenUpdating = False
  Application.FindFormat.Clear
  Application.FindFormat.Interior.Color = RGB(192, 192, 192)
  With Columns("E")
    With Intersect(.Cells, ActiveSheet.UsedRange)
      .Value = .Value
    End With
    .Cells.Replace "*", "", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    .SpecialCells(xlBlanks).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.FindFormat.Clear
  Application.ScreenUpdating = True
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
How would the code change if the cells got their color by conditional formatting?
Thanks!!!
 
Upvote 0

Forum statistics

Threads
1,215,569
Messages
6,125,600
Members
449,238
Latest member
wcbyers

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