过滤一列上的空白并填写另一列中的值

时间:2014-01-22 10:48:52

标签: excel vba excel-vba

我已经有了这个宏来过滤掉工作簿中的空白和循环

Sub Filter()


SearchCol = "Branch ID"
Dim ws As Object

For Each ws In Worksheets
Set rng1 = ws.UsedRange.Find(SearchCol, , xlValues, xlWhole)
With ws.Range("A2")
    .AutoFilter field:=rng1.Column, Criteria1:="<>"
End With

Next ws

End Sub

完成此操作后,我现在想要搜索另一列,并应用此过滤器,并将该列中的所有单元格填充为红色,其值为&lt; 4。所以它需要遍历第二个搜索到的名为“amount”的列并填充“amount”&lt; 4红色并为所有工作表执行此操作

此外,我需要展示如何在此标记问题,我可能是一个白痴

由于

1 个答案:

答案 0 :(得分:0)

尝试下一个:

Sub test()
    Dim SearchCol As String
    Dim rng1 As Range
    Dim rng2 As Range
    Dim ws As Worksheet

    SearchCol = "ID"

    For Each ws In Worksheets
        Set rng1 = ws.UsedRange.Find(SearchCol, , xlValues, xlWhole)
        Set rng2 = ws.UsedRange.Find("Amount", , xlValues, xlWhole)
        If Not rng1 Is Nothing Then
            With ws
                .Range("A2").AutoFilter field:=rng1.Column, Criteria1:="<>"
                .Range("A2").AutoFilter field:=rng2.Column, Criteria1:="<4"
                On Error Resume Next
                lastRow = .Cells(.Rows.Count, rng2.Column).End(xlUp).Row
                .Range(.Cells(rng2.Row + 1, rng2.Column), .Cells(lastRow, rng2.Column)).SpecialCells(xlCellTypeVisible).Interior.Color = RGB(255, 0, 0)
                On Error GoTo 0
            End With
        End If
    Next ws
End Sub