我在 excel 中有一个大约 20000 行和 4 列的列表。此 Excel 表包含粗体名称,之后的列包含有关它们的信息。每个名称后面都有一些多余的信息,占据 3 或 4 行,但并不一致。我需要遍历工作表并删除所有没有粗体名称的行。
问问题
1936 次
2 回答
1
Sub deleteNonBolded()
Dim cell As Range
Dim selectRange As Range
For Each cell In Intersect(ActiveSheet.Range("A:A"), ActiveSheet.UsedRange)
If (cell.Font.Bold = False) Then
If selectRange Is Nothing Then
Set selectRange = cell
Else
Set selectRange = Union(cell, selectRange)
End If
End If
Next cell
selectRange.EntireRow.Delete
End Sub
于 2013-02-01T04:17:47.400 回答
1
您需要创建一个宏来找出当前工作表中有多少行,然后遍历从工作表底部到顶部的行,检查Font.Bold
该行第一列的属性是否设置为 false . 如果是这样,您删除该行。
以下对我有用:
Sub DeleteUnboldRows()
Dim lastRow As Long
Dim currentRow As Long
'Select All the rows in the active worksheet
lastRow = ActiveSheet.UsedRange.Rows.Count
' Iterate through each row from the bottom to the top.
' If we go the other way rows will get skipped as we delete unbolded rows!
For currentRow = lastRow To 1 Step -1
'Look at the cell in the first column of the current row
' if the font is not bolded delete the row
If ActiveSheet.Rows(currentRow).Columns(1).Font.Bold = False Then
ActiveSheet.Rows(currentRow).Delete
End If
Next currentRow
End Sub
这是该Bold
属性的参考:http: //msdn.microsoft.com/en-us/library/office/aa224034%28v=office.11%29.aspx
于 2013-02-01T04:09:53.580 回答