VBA - Delete rows that DO NOT contain Bold font

JamesonMH

Board Regular
Joined
Apr 17, 2018
Messages
120
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I know this is a pretty easy one, but can't get this to work. I have a bunch of data rows and if all the cells are not Bold in a row, then delete the row (just delete within data range, not entire row). Otherwise, if there is a cell in the row that is Bold, then leave row as is.

[TABLE="width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Code[/TD]
[/TR]
[TR]
[TD]Store 1[/TD]
[TD]500[/TD]
[/TR]
[TR]
[TD]Store 3[/TD]
[TD]275
[/TD]
[/TR]
[TR]
[TD]Store 7[/TD]
[TD]480[/TD]
[/TR]
[TR]
[TD]Store 2[/TD]
[TD]450[/TD]
[/TR]
</tbody>[/TABLE]

With above example, I'd only want the Store 3 & Store 7 rows to remain (excluding headers).

Thanks!
James
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You did not tell us what the range is nor the starting row for your data (excluding the header), so I guessed... change the red highlighted parts as needed...
Code:
Sub DeleteRowsIfAllCellsAreNotBold()
  Dim R As Long, StartRow As Long, EndRow As Long, DataCols As String
  DataCols = "[B][COLOR="#FF0000"]D:E[/COLOR][/B]"
  StartRow = [B][COLOR="#FF0000"]2[/COLOR][/B]
  EndRow = Cells(Rows.Count, Columns(DataCols).Resize(, 1).Column).End(xlUp).Row
  For R = StartRow To EndRow
    If Intersect(Rows(R), Columns(DataCols)).Font.Bold = False Then Intersect(Rows(R), Columns(DataCols)).Delete xlShiftUp
  Next
End Sub
 
Upvote 0
Hi,
You can also try this.

Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub DeleteNotBold()
    Dim headerColumn&
    Dim headerRow&
    Dim lastRow&
    Dim i&
    
    
    headerColumn = 1
    headerRow = 1
    
    lastRow = Cells(Rows.Count, headerColumn).End(xlUp).Row
    For i = lastRow To headerRow + 1 Step -1
        If Cells(i, headerColumn).Font.Bold = False And Cells(i, headerColumn).Offset(0, 1).Font.Bold = False Then
            Range(Cells(i, headerColumn), Cells(i, headerColumn).Offset(0, 1)).Delete
        End If
    Next i
    
End Sub[/FONT]
 
Upvote 0
You did not tell us what the range is nor the starting row for your data (excluding the header), so I guessed... change the red highlighted parts as needed...
Code:
Sub DeleteRowsIfAllCellsAreNotBold()
  Dim R As Long, StartRow As Long, EndRow As Long, DataCols As String
  DataCols = "[B][COLOR=#FF0000]D:E[/COLOR][/B]"
  StartRow = [B][COLOR=#FF0000]2[/COLOR][/B]
  EndRow = Cells(Rows.Count, Columns(DataCols).Resize(, 1).Column).End(xlUp).Row
  For R = StartRow To EndRow
    If Intersect(Rows(R), Columns(DataCols)).Font.Bold = False Then Intersect(Rows(R), Columns(DataCols)).Delete xlShiftUp
  Next
End Sub

Sorry about the range/column omission. This works perfectly - thanks very much Rick! Question, what would I tweak in that code if I ever needed to delete the entire row instead of only what we're deleting right now?
 
Upvote 0
Hi,
You can also try this.

Code:
[FONT=Verdana]Sub DeleteNotBold()
    Dim headerColumn&
    Dim headerRow&
    Dim lastRow&
    Dim i&
    
    
    headerColumn = 1
    headerRow = 1
    
    lastRow = Cells(Rows.Count, headerColumn).End(xlUp).Row
    For i = lastRow To headerRow + 1 Step -1
        If Cells(i, headerColumn).Font.Bold = False And Cells(i, headerColumn).Offset(0, 1).Font.Bold = False Then
            Range(Cells(i, headerColumn), Cells(i, headerColumn).Offset(0, 1)).Delete
        End If
    Next i
    
End Sub[/FONT]

Thanks for your reply Mentor82 - I'm going to work through your code this aft to understand it. I like how it's a different approach to Rick's solution. Perhaps I'll be able to use your technique in the future.
 
Upvote 0
Sub DeleteRowsIfAllCellsAreNotBold()
Dim R As Long, StartRow As Long, EndRow As Long, DataCols As String
DataCols = "D:E"
StartRow = 2
EndRow = Cells(Rows.Count, Columns(DataCols).Resize(, 1).Column).End(xlUp).Row
For R = StartRow To EndRow
If Intersect(Rows(R), Columns(DataCols)).Font.Bold = False Then Intersect(Rows(R), Columns(DataCols)).Delete xlShiftUp
Next
End SubSorry about the range/column omission. This works perfectly - thanks very much Rick! Question, what would I tweak in that code if I ever needed to delete the entire row instead of only what we're deleting right now?
Replace what I highlighted in red above to this...
Code:
Rows(R).Delete xlShiftUp
 
Upvote 0
You're welcome!
With my approach you need to set first header column no which I set to 1, header row no which I also set to 1 and offset(0,1) which I set to one because I assume you have two columns. If you have more columns ex 4 change it to 3 etc.
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,712
Members
452,995
Latest member
isldboy

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