简体   繁体   English

基于值是否在文本列表中的VBA条件格式单元格

[英]VBA Conditional format cell based on whether value is in list of text

I have this code: 我有以下代码:

Sub Japan()

Set MyPlage = Range("A1:R1000")
For Each Cell In MyPlage

    If Cell.Value = "A" Then
        Rows(Cell.Row).Interior.ColorIndex = 3
    End If

    If Cell.Value = "B" Then
        Rows(Cell.Row).Interior.ColorIndex = 3
    End If

    If Cell.Value = "C" Then
        Rows(Cell.Row).Interior.ColorIndex = 3
    End If

    If Cell.Value = "D" Then
        Rows(Cell.Row).Interior.ColorIndex = 3
    End If

    If Cell.Value = "E" Then
        Rows(Cell.Row).Interior.ColorIndex = 3
    End If

Next

End Sub

THis find any cells that have either A, B, C, D, E as the value and then colours the entire row red if so. 这将找到具有A,B,C,D,E作为值的任何单元格,然后将整个行都涂成红色。

Basically, I have hundreds of more values that I want to lookup. 基本上,我还有数百个要查找的值。 I have them stored in another excel file (could just as easily be in a text file). 我将它们存储在另一个excel文件中(可以很容易地将其存储在文本文件中)。 How could I reference them? 我该如何引用它们? ie, if cell value is in this list of text, do this. 即,如果单元格值在此文本列表中,请执行此操作。

Sounds like you want a Set datastructure that contains unique values and you can use an Exist method on it. 听起来您想要一个包含唯一值的Set数据结构 ,并且可以在其上使用Exist方法。

For example your desired usage is this. 例如,您所需的用法是这样。

Set MySet = LoadRedValueSet(???) ' explain later
Set MyPlage = Range("A1:R1000")
For Each Cell In MyPlage
    If MySet.Exists(Cell.Value) Then
        Rows(Cell.Row).Interior.ColorIndex = 3
    End If
Next

Well too bad Set is a reserved keyword and VBA does not provide a Set object. 太糟糕了, Set是一个保留关键字,VBA不提供Set对象。 However, it does provide a Dictionary object which can be abused like a Set would be. 但是,它确实提供了一个Dictionary对象,可以像Set那样滥用它。 You will need to reference the Scripting Runtime Library to use it first through . 您需要参考Scripting Runtime Library才能首先使用它 The usage would be exactly as stated as above. 用法将完全如上所述。 But first we need to define LoadRedValueSet() 但是首先我们需要定义LoadRedValueSet()

Lets assume that you are able to load whatever file you save these values as in as an Excel worksheet. 假设您能够加载将这些值保存为Excel工作表的任何文件。 I will not be explaining how to open various file types in Excel as there are many answers detailing that in more detail than I can. 我将不会解释如何在Excel中打开各种文件类型,因为有许多答案比我能详细地详细说明。 But once you have your range of values to add to the set we can add them to the dictionary. 但是一旦您将值的范围添加到集合中,我们就可以将它们添加到字典中。

Private Function LoadRedValueSet(valueRange As Range) As Dictionary

    Dim result As New Dictionary

    Dim cell As Range
    For Each cell In valueRange.Cells
       result(cell.value) = Nothing
    Next cell

    Set LoadRedValueSet = result

End Function

Dictionary are mapping objects that have key->value pairs. 字典是具有键->值对的映射对象。 The key's are effectively a set, which is what we want. 密钥实际上是我们想要的一组。 We don't care about the values and you can pass whatever you want to it. 我们不在乎值,您可以将任何想要的值传递给它。 I used Nothing . Nothing If you use the .Add method the dictionary will throw an error if your list contains duplicate entries. 如果使用.Add方法,则如果列表中包含重复的条目,则字典将引发错误。

Assuming you have implemented some function that loads your file as a worksheet and returns that worksheet. 假设您实现了一些功能,可以将文件作为工作表加载并返回该工作表。

Dim valueSheet As Worksheet
Set valueSheet = LoadSomeFileTypeAsWorksheet("some file path")

Dim valueRange As Range
Set valueRange = valueSheet.??? 'column A or whatever

Dim MyDictAsSet As Dictionary
Set MyDictAsSet = LoadRedValueSet(valueRange)

Set MyPlage = Range("A1:R1000")
For Each Cell In MyPlage
    If MyDictAsSet.Exists(Cell.Value) Then
        Rows(Cell.Row).Interior.ColorIndex = 3
    End If
Next

There are quite a few ways you could possibly do this but here's my approach. 您可以通过多种方式执行此操作,但这是我的方法。 Application.WorksheetFunction.<function name> can be used to evaluate worksheet functions within VBA. Application.WorksheetFunction.<function name>可用于评估VBA中的工作表功能。 This means we can use it to run a Match function. 这意味着我们可以使用它来运行Match函数。 For the sake of a simple example let's assume your values to match are in Column A of a worksheet called Sheet2 (in the same workbook). 为了简单起见,让我们假设要匹配的值在名为Sheet2的工作表的A列中(在同一工作簿中)。

Dim MyPlage As Range, Cell As Range
Dim result as Variant
Set MyPlage = Range("A1:R1000") '<~~ NOTE: Sheets("<SheetName>").Range("A1:R1000") would be better

For Each Cell in MyPlage
    result = Application.WorksheetFunction.Match(Cell.Value, Sheets("Sheet2").Range("A:A"), 0)
    If Not IsError(result) Then
        Rows(Cell.Row).Interior.ColorIndex = 3
    End If
Next Cell

We only need to know whether or not the WorksheetFunction.Match function returned an error: If it didn't then Cell.Value was present in Column A of Sheet2 and we color the row red. 我们只需要知道WorksheetFunction.Match函数是否返回错误:如果没有返回,则Sheet2的A列中存在Cell.Value,我们将行涂成红色。

Paste your color value + index data to a new sheet called "Colors" in the following order; 按以下顺序将您的颜色值+索引数据粘贴到名为“ Colors”的新工作表中;

Value   ColorIndex
A       1
B       2
C       3
D       4
E       5

And update your method with the following code and update the range based your data; 并使用以下代码更新您的方法,并根据您的数据更新范围;

Sub SetColors()

    ' DataCells: The cells that's going to be checked against the color values
    Set DataCells = Range("A1:A15") ' Update this value according to your data cell range

    ' ColorValueCells: The cells that contain the values to be colored
    Set ColorValueCells = Sheets("Colors").Range("A2:A6") ' Update this value according to your color value + index range

    ' Loop through data cells
    For Each DataCell In DataCells

        ' Loop through color value cells
        For Each ColorValueCell In ColorValueCells

            ' Search for a match
            If DataCell.Value = ColorValueCell.Value Then

                ' If there is a match, find the color index
                Set ColorIndexCell = Sheets("Colors").Range("B" & ColorValueCell.Row)

                ' Set data cell's background color with the color index
                DataCell.Interior.ColorIndex = ColorIndexCell.Value

            End If
        Next
    Next
End Sub

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

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