Written by Allen Wyatt (last updated May 11, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
Tara often receives workbooks containing a single worksheet where some information in the worksheet contains text that is "struck through" (the text is formatted using strikethrough). Rows that contain struck-through text need to be deleted, but Tara isn't sure of how to quickly go about this.
If you only need to do this task once in a while, the following steps will help you out if it is easy to see which cells contain the strike-through formatting:
If it is not that easy to see all the rows containing struck-through text, you can rely on the Find feature to locate them for you:
There is a drawback to this approach—it won't find any cells containing mixed formatting. In other words, if only some of the text within the cell uses the strikethrough attribute, then that cell won't be found in step 8.
This deficiency can be overcome by using a macro. Plus, a macro is a good approach if you need to perform this task quite often. Here's one that will do the trick:
Sub DeleteSTRows() Dim c As Range Dim bCheck As Boolean Dim J As Integer Dim iRows As Integer iRows = Selection.Rows.Count If iRows > 2 Then For J = iRows To 1 Step -1 bCheck = False For Each c In Selection.Rows(J).Cells bCheck = IsNull(c.Font.Strikethrough) If Not bCheck Then bCheck = c.Font.Strikethrough If bCheck Then Exit For Next c If bCheck Then Selection.Rows(J).EntireRow.Delete Next J End If End Sub
To use the macro, simply select the cells you want to affect, and then run it. The macro steps through each row of the selection (from the last row to the first) and then checks each cell within those rows. If the cell contains struck-through text, then a flag (bCheck) is set and the entire row is deleted. (The macro won't work if your selection contains only 1 or 2 rows—that few of rows is easy enough to check and adjust manually.)
Note in the middle of the macro that the IsNull function is used. This is the part that detects if the cell contains mixed formatting. If only some of the characters in the cell use strike-through, then a null value is returned by the Strikethrough property. This is checked by IsNull and assigned to the bCheck variable. If bCheck is still False (meaning that there is no mixed use of strike-through in the cell), then the entire cell is checked to see if it has the requisite formatting.
It should be noted that if you don't want the macro to actually delete rows, but would prefer to just clear them, you can replace the .Delete method in the macro with the .Clear method.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5270) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Paste information in a worksheet, and you may end up with Excel placing it into lots of different cells. If you want it ...
Discover MoreEnter information into a cell, and Excel needs to figure out what type of information it is. Here's how Excel interprets ...
Discover MoreWhen setting up a worksheet for others to use, you might want to make some limitations on what can be entered in certain ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-05-15 10:04:04
J. Woolley
Re. the Tip's macro, I have some nits to pick:
1. From the Tip: "The macro won't work if your selection contains only 1 or 2 rows—that few of rows is easy enough to check and adjust manually." Is this really necessary?
2. A cell formatted with Strikethrough font will trigger deletion even if that cell is empty or blank ("").
3. Rows containing Strikethrough font are deleted without confirmation even if they were originally hidden.
4. Possibly hidden columns are not considered.
The following version addresses these issues:
Sub DeleteSTRows2()
Dim sele As Range, cell As Range, nCols As Long, n As Long
Dim hideCols() As Boolean, hideRow As Boolean, strike As Variant
If Not (TypeOf ActiveSheet Is Worksheet) Then Exit Sub
Set sele = Application.Intersect(Selection, ActiveSheet.UsedRange)
If sele Is Nothing Then Exit Sub
nCols = sele.Columns.Count
ReDim hideCols(nCols)
For n = 1 To nCols
hideCols(n) = sele.Columns(n).Hidden 'original status
sele.Columns(n).Hidden = False 'make visible
Next n
For n = sele.Rows.Count To 1 Step -1
With sele.Rows(n)
For Each cell In .Cells 'for each cell in this row
If cell.Value <> "" Then
strike = cell.Font.Strikethrough
If strike Or IsNull(strike) Then 'full or partial
hideRow = .Hidden 'original status
.Hidden = False 'make visible
.Cells(1).Show
.Select
If MsgBox("Delete this row?", vbYesNo) = vbYes Then
.EntireRow.Delete
Else
.Hidden = hideRow 'restore original
End If
Exit For 'finished with this row
End If
End If
Next cell
End With
Next n
For n = 1 To nCols
sele.Columns(n).Hidden = hideCols(n) 'restore original
Next n
sele.Select 'remainder of original selection
End Sub
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments