简体   繁体   English

vba到自动过滤器,使用“ <>”作为过滤字体的条件

[英]vba to Autofilter using “<>” for criteria to filter on fontcolor

This is snippet of my code: 这是我的代码的片段:

Dim col As Integer
col = InputBox("Please enter column number to search", "Need Input", 0)
ActiveSheet.AutoFilterMode = False

With ActiveSheet
Set rnData = .UsedRange
    With rnData
        .AutoFilter Field:=col, Criteria1:="<>RGB(0, 0, 255)", Operator:=xlFilterFontColor      
    End With
End With

This throws an error but works when I remove <> and "" to select blue font but I want non blue font. 这将引发错误,但是当我删除<>和“”以选择蓝色字体但我想要非蓝色字体时可以使用。 Questions: The col that I want to filter has some sentences in which few words have different font Colors(red,blue,green...) or all just black 1) I want to filter on non blue color. 问题:我要过滤的列中有一些句子,其中有几个单词具有不同的字体颜色(红色,蓝色,绿色...)或全是黑色1)我要过滤非蓝色颜色。 2) Filter on only black fontcolor with no words being of different color.how do I achieve that? 2)仅过滤黑色fontcolor,而没有不同颜色的单词。如何实现? 3) Is there a way to give the colummn as alphabets instead of column number- basically does the field accept alphabets instead of numbers to indicate column. 3)有没有一种方法可以将列作为字母而不是列号-基本上该字段接受字母而不是数字来表示列。

When i used @jeeped code for an eg 当我将@jeeped代码用于例如 在此处输入图片说明

after running the code then it gives this 运行代码后,它给出了 在此处输入图片说明

which is what I am not clear why it is not able find non blue. 这是我不清楚为什么它无法找到非蓝色。

In the past, I've used a method of collecting all non-conforming (ie non-blue ) values from the filter field into an array and then filtering on the array. 过去,我曾使用过一种方法,将所有不合格(即非蓝色 )值从过滤器字段收集到一个数组中,然后在该数组上进行过滤。 Your problem seemed similar so I adapted the procedure for the filter field's font color. 您的问题似乎很相似,因此我针对过滤器字段的字体颜色调整了程序。

Sub Not_Blue()
    Dim r As Long, iFLD  As Long
    Dim dNOTBLUs As Object    'New Scripting.Dictionary

    Set dNOTBLUs = CreateObject("Scripting.Dictionary")
    dNOTBLUs.CompareMode = vbTextCompare

    With ActiveSheet.Cells(1, 1).CurrentRegion
        If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False

        iFLD = 3   ' get your filter column here
        For r = 2 To .Cells(Rows.Count, iFLD).End(xlUp).Row
            '~~> collect unique non-blue values
            If .Cells(r, iFLD).Font.Color <> RGB(0, 0, 255) Then _
                dNOTBLUs.Add Key:=.Cells(r, iFLD).Text, Item:=.Cells(r, iFLD).Value2
        Next r

        .AutoFilter Field:=iFLD, Criteria1:=dNOTBLUs.Keys, Operator:=xlFilterValues

        'column iFLD is filtered for all non-blur fonts
        'do something with the filtered results here

    End With

    dNOTBLUs.RemoveAll: Set dNOTBLUs = Nothing
End Sub

That collects a unique list of all non-blue values into a scripting dictionary. 它将所有非蓝色值的唯一列表收集到脚本字典中。 It uses the unique key list as the array to filter on. 它使用唯一列表作为要过滤的数组。 Essentially, you are abandoning the xlFilterFontColor but achieving the same results. 本质上,您放弃了xlFilterFontColor但获得了相同的结果。

按颜色自动过滤
Before 之前

自动筛选不上色结果
After


If you can convert the late binding of the Scripting.Dictionary to early binding, you must add Microsoft Scripting Runtime to the VBE's Tools ► References. 如果可以将Scripting.Dictionary的后期绑定转换为早期绑定,则必须将Microsoft Scripting Runtime添加到VBE的“工具”►“引用”中。

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

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