简体   繁体   English

如果第一列中的单元格不是粗体,如何删除excel中的行?

[英]How to delete rows in excel if the cell in the first column isn't bold?

I have a list in excel with about 20000 rows and 4 columns. 我在excel中有一个列表,大约有20000行和4列。 This excel sheet contains names in bold, and the columns after that have information about them. 此excel表包含粗体名称,之后的列包含有关它们的信息。 After each name there is some excess information that takes up either 3 or 4 rows, but it's not consistent. 在每个名称之后,有一些超出的信息占用了3行或4行,但它并不一致。 I need to run through the sheet and delete all the rows where there isn't a bold name. 我需要遍历工作表并删除没有粗体名称的所有行。

You need to create a macro the finds out how many rows are in the current worksheet and that then iterates through the rows from the bottom of the worksheet to the top checking to see if the Font.Bold property on the first column of the row is set to false. 您需要创建一个宏来查找当前工作表中有多少行,然后遍历工作表底部的行到顶部检查以查看行的第一列上的Font.Bold属性是否为设为false。 If so you delete that row. 如果是这样,您删除该行。

The following works for me: 以下适用于我:

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

Here is a reference for the Bold property: http://msdn.microsoft.com/en-us/library/office/aa224034%28v=office.11%29.aspx 以下是Bold属性的参考: http//msdn.microsoft.com/en-us/library/office/aa224034%28v=office.11​​%29.aspx

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM