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.

NameCode
Store 1500
Store 3275
Store 7480
Store 2450

<tbody>
</tbody>

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

Thanks!
James
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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,213,560
Messages
6,114,304
Members
448,564
Latest member
ED38

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