简体   繁体   English

VBA - 删除包含黑色文本的范围中的每个单元格的行

[英]VBA - Remove rows that have every cell in the range that contain black text

I've been tasked to analyse a workbook where I need to isolate the data based on the colour (red or black) that the text is in relating to the rows. 我的任务是分析一个工作簿,我需要根据文本与行相关的颜色(红色或黑色)来隔离数据。

I essentially need to develop a macro that will remove all the rows that contain data (text) that is 'all black' in the range (column CJ) and leave all the rows that contain at least one cell in the range (column CJ) that contains text that is 'red' (255,0,0). 我基本上需要开发一个宏,它将删除包含范围内所有黑色的数据(文本)的所有行(列CJ),并保留包含该范围中至少一个单元格的所有行(列CJ)包含'red'(255,0,0)的文本。

The completed result should be that every row will contain at least one cell that contains red text between between Column CJ. 完成的结果应该是每行至少包含一个在CJ列之间包含红色文本的单元格。

The data is set our as follows: 数据设置如下:

Names: 名称:

A1,B1 A1,B1

A2,B2 all the way to A2,B2一直到

A2000,B2000 A2000,B2000

Data (text) is set up like the following: 数据(文本)设置如下:

C1 to J1 C1到J1

C2 to J2 all the way to C2到J2一直到

C2000, J2000 C2000,J2000

I've found numerous codes that conditionally colour format but I can't seem to develop one that does what I want above. 我发现了许多有条件颜色格式的代码,但我似乎无法开发出符合我上面要求的代码。

Any help will be greatly appreciated. 任何帮助将不胜感激。

You could try: 你可以尝试:

Option Explicit

Sub test()

    Dim i As Long

    With ThisWorkbook.Worksheets("Sheet1")

        For i = 2000 To 2 Step -1

            If .Range("C" & i).Value = "" And .Range("D" & i).Value = "" And .Range("E" & i).Value = "" And .Range("F" & i).Value = "" _
                And .Range("G" & i).Value = "" And .Range("H" & i).Value = "" And .Range("I" & i).Value = "" And .Range("J" & i).Value = "" Then

                .Rows(i).Delete

            End If

        Next i

    End With

End Sub

You can use AutoFilter to filter by font color. 您可以使用AutoFilter按字体颜色进行过滤。 It does not matter whether the color was derived by manual formatting or conditional formatting. 颜色是通过手动格式化还是条件格式化得出并不重要。

In your case, you are 'proofing a negative' across many columns. 在您的情况下,您在许多列中“证明负面”。 A helper column appears necessary. 有必要使用辅助列。 The code below cycles through columns C:J and marks the 'helper' column every time it encounters filtered rows with a red font. 下面的代码循环遍历列C:J并在每次遇到带有红色字体的过滤行时标记“帮助”列。

Sub anyRedFont()

    Dim c As Long

    With Worksheets("sheet1")

        'remove any AutoFilters
        If .AutoFilterMode Then .AutoFilterMode = False

        'insert a 'helper' column and label it
        .Columns("C").Insert
        .Cells(1, "C") = "helper"

        'filter for red font color
        With .Range(Cells(1, "C"), .Cells(.Rows.Count, "K").End(xlUp))

            'cycle through columns looking for red font
            For c = 2 To 9

                'fliter for red font
                .AutoFilter Field:=c, Criteria1:=vbRed, _
                            Operator:=xlFilterFontColor, VisibleDropDown:=False

                'put a value into the 'helper' column
                On Error Resume Next
                With .Resize(.Rows.Count - 1, 1).Offset(1, 0)
                    Debug.Print .SpecialCells(xlCellTypeVisible).Address(0, 0)
                    .SpecialCells(xlCellTypeVisible) = 1
                End With
                On Error GoTo 0

                'remove fliter for red font
                .AutoFilter Field:=c

            Next c

            'fliter for non-blank helper column
            .AutoFilter Field:=1, Criteria1:=1, VisibleDropDown:=False

        End With

        'Do your work with the rows containing at least one cell
        'with red font here

        'remove 'helper' column
        'this removes the AutoFilter since the 'helper' column
        'is the primary filter column at this point
        '.Columns(Application.Match("helper", .Rows(1), 0)).Delete

        'remove AutoFilter (manually with Data, Data Tools, Clear)
        'If .AutoFilterMode Then .AutoFilterMode = False

    End With

End Sub

I've commented out removing the 'helper' column. 我已经注释掉删除'帮助'列。 The 'helper' is the primary filter column so removing it also removes the AutoFilter. 'helper'是主过滤器列,因此删除它也会删除AutoFilter。

I may as well offer another opinion, just for fun. 我也可以提供另一种意见,只是为了好玩。 :-) :-)

Copy and paste the below into a new module, select the area of cells you want to run this over and then execute the macro. 将下面的内容复制并粘贴到新模块中,选择要在其上运行的单元格区域,然后执行宏。

Public Sub RemoveAllRowsWithBlackText()
    Dim rngCells As Range, bFoundNonBlack As Boolean, lngRow As Long
    Dim lngCol As Long

    Set rngCells = Selection

    Application.ScreenUpdating = False

    With rngCells
        For lngRow = .Rows.Count To 1 Step -1
            bFoundNonBlack = False

            For lngCol = 1 To .Columns.Count
                If .Cells(lngRow, lngCol).Font.Color <> 0 And Trim(.Cells(lngRow, lngCol)) <> "" Then
                    bFoundNonBlack = True
                    Exit For
                End If
            Next

            If Not bFoundNonBlack Then
                .Cells(lngRow, lngCol).EntireRow.Delete xlShiftUp
            End If
        Next
    End With

    Application.ScreenUpdating = True
End Sub

... it's not bound to your columns, it will move with the selection you make. ...它没有绑定到您的列,它将随您所做的选择而移动。

选择单元格

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

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