【问题标题】:Using match function with colour criteria使用带有颜色标准的匹配功能
【发布时间】:2017-08-30 12:04:10
【问题描述】:

我正在尝试使用 match 查找单元格的行。我已经能够做到这一点

rowfound = Application.WorksheetFunction.Match("123", Range("A:A"), 0)

但这就是我被困的地方。 “123”可能在同一列中出现了多次(这些已经用颜色填充了),我正在尝试找到最新的“123”单元格。此单元格不会填充任何颜色。

我尝试过以这种方式输入,但我相信 Interior.ColorIndex = 0 仅适用于对象而非范围。

rowfound = Application.WorksheetFunction.Match("123", Range("A:A").Interior.ColorIndex = 0, 0)

我也尝试在未着色的单元格上执行Selection.Address 并从那里进行匹配,但这将导致匹配函数给出的结果不是单元格在工作表中所在的行(它'将给出可以在选择中找到的行)[即“123”的实际单元格行= 2000,但给出的结果是“1”,这是选择中的行]。我的代码是

RRR = Selection.Address
rowfound = Application.WorksheetFunction.Match("123", Range(RRR), O)

知道如何解决这个问题吗?希望我对我的问题的解释很清楚。

【问题讨论】:

  • 不确定是什么让您认为可以这样使用 Match。不过,您可以使用带有格式的 Find 方法。
  • 使用 Range.Find 和 SearchFormat 参数来查找相关单元格

标签: vba excel colors find match


【解决方案1】:

您可以使用 Range.Find 的“After”参数继续前进,直到匹配所需的 Interior.Color。

我做了一个小sn-p来说明我的意思:

Sub test()
Dim blankCode As Long
Dim tempRange As Range
Dim lookupValue As String

blankCode = 16777215 'Blank cell
lookupValue = "testing this out" 'what im looking for
Set tempRange = Range("A1") 'Starting in cell A1

Do
'Going through each match of the lookup value until the color is what we need
    Set tempRange = Range("A:A").Find(lookupValue, After:=tempRange)
    If tempRange.Interior.Color = blankCode Then
        MsgBox ("Found it on cell " & tempRange.Address)
        Exit Do
    End If

'This will loop indefinitely if the value isn't there, so I'd add a loop control that suits your project
Loop While True

End Sub

【讨论】:

    【解决方案2】:

    我将我的解决方案作为一个与常规 Match 函数非常相似的函数:

    Public Function MatchUncolored(lookup_value, lookup_array As Range)
    Dim cell As Range, ct As Long
    
    For Each cell In Union(lookup_array.SpecialCells(xlCellTypeConstants), lookup_array.SpecialCells(xlCellTypeFormulas))
    
        ct = ct + 1
    
        If ct > 10000 Then
            MatchUncolored = "#ERROR"
            Exit Function
        End If
    
        If cell.Value = lookup_value And cell.Interior.ColorIndex = xlNone Then
            MatchUncolored = ct
            Exit Function
        End If
    
    Next cell
    
    End Function
    

    似乎适用于我测试过的东西,但你的里程可能会有所不同。

    【讨论】:

      【解决方案3】:

      考虑:

      Application.FindFormat.Interior.ColorIndex = 0
      rowfound = Range("A:A").Find("123", SearchFormat:=True)
      

      【讨论】:

        【解决方案4】:

        这是另一种方法。这个使用 Find 方法来搜索匹配项。此外,它也使用一个函数来返回行号。但是,如果没有匹配,它会返回一个可以测试的错误。

        Option Explicit
        
        Sub test()
            Dim vRow As Variant
            vRow = FindUncolored("123", Range("A:A"))
            If IsError(vRow) Then
                MsgBox "No match found!", vbInformation
            Else
                MsgBox "Match found in Row " & vRow, vbInformation
            End If
        End Sub
        
        Function FindUncolored(strSearchFor As String, rngRange As Range)
        
            Dim rngFound As Range
        
            With Application.FindFormat
                .Clear
                .Interior.ColorIndex = xlNone
            End With
        
            With rngRange
                Set rngFound = .Find(what:=strSearchFor, after:=.Cells(.Rows.Count), LookIn:=xlValues, _
                    lookat:=xlWhole, searchorder:=xlRows, searchdirection:=xlNext, MatchCase:=False, SearchFormat:=True)
            End With
        
            If Not rngFound Is Nothing Then
                FindUncolored = rngFound.Row
            Else
                FindUncolored = CVErr(xlErrNA)
            End If
        
            Application.FindFormat.Clear
        
        End Function
        

        希望这会有所帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-11-05
          • 2020-05-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-07
          相关资源
          最近更新 更多