【问题标题】:VBA - Remove rows that have every cell in the range that contain black textVBA - 删除范围内每个单元格都包含黑色文本的行
【发布时间】:2019-03-27 06:15:19
【问题描述】:

我的任务是分析一个工作簿,我需要根据文本与行相关的颜色(红色或黑色)来隔离数据。

我基本上需要开发一个宏,该宏将删除范围内(C-J 列)中包含“全黑”数据(文本)的所有行,并保留范围内至少包含一个单元格的所有行(列 C-J),其中包含“红色”(255,0,0) 文本。

完成的结果应该是每一行将包含至少一个单元格,其中包含 C-J 列之间的红色文本。

我们的数据设置如下:

姓名:

A1,B1

A2,B2一路

A2000,B2000

数据(文本)设置如下:

C1 到 J1

C2到J2一路

C2000、J2000

我找到了许多有条件地设置颜色格式的代码,但我似乎无法开发出符合我上述要求的代码。

任何帮助将不胜感激。

【问题讨论】:

标签: excel vba colors row


【解决方案1】:

我不妨提供另一种意见,只是为了好玩。 :-)

将以下内容复制并粘贴到一个新模块中,选择要运行它的单元格区域,然后执行宏。

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

...它没有绑定到您的列,它会随着您所做的选择而移动。

【讨论】:

  • 这段代码几乎可以完成这项工作!有时我的数据有我发现的空白单元格,所以需要删除有黑色文本单元格和空白单元格的行?
  • @RailMan 好的,但我不确定我是否遵循。即使您有空白单元格,如果所有单元格的文本颜色均为黑色,则应删除该行。这没有发生吗?
  • 是的,我的意思是有些行的单元格包含黑色文本,而单元格只是空白。这些行仍然存在(但我需要将它们删除)。到目前为止,仅删除了每个单元格都包含黑色文本的行。那些有意义的?
  • @RailMan 是的,100%。单元格本身,如果不被触摸,则不得设置黑色......或其他东西。我会尽快回复您。
  • @RailMan 我更新了答案。您可能需要删除并放回双引号,我在手机上更改了答案,它们看起来有点奇怪。
【解决方案2】:

你可以试试:

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

【讨论】:

    【解决方案3】:

    您可以使用 AutoFilter 按字体颜色进行过滤。颜色是通过手动格式还是条件格式导出的并不重要。

    在您的情况下,您在许多列中“校对否定”。辅助列似乎是必要的。下面的代码在 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
    

    我已注释掉删除“助手”列。 'helper' 是主要过滤器列,因此删除它也会删除自动过滤器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      相关资源
      最近更新 更多