我的任务是分析一个工作簿,我需要根据文本与行相关的颜色(红色或黑色)来隔离数据。
我基本上需要开发一个宏,它将删除包含范围内所有黑色的数据(文本)的所有行(列CJ),并保留包含该范围中至少一个单元格的所有行(列CJ)包含'red'(255,0,0)的文本。
完成的结果应该是每行至少包含一个在C-J列之间包含红色文本的单元格。
数据设置如下:
名称:
A1,B1
A2,B2一直到
A2000,B2000
数据(文本)设置如下:
C1到J1
C2到J2一直到
C2000,J2000
我发现了许多有条件颜色格式的代码,但我似乎无法开发出符合我上面要求的代码。
任何帮助将不胜感激。
我也可以提供另一种意见,只是为了好玩。 :-)
将下面的内容复制并粘贴到新模块中,选择要在其上运行的单元格区域,然后执行宏。
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
...它没有绑定到您的列,它将随您所做的选择而移动。
你可以尝试:
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
您可以使用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'是主过滤器列,因此删除它也会删除AutoFilter。