我正在尝试使用匹配来查找单元格的行。我已经能够用
做到这一点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)
知道如何解决这个问题吗?希望我对我的问题的解释很清楚。
答案 0 :(得分:0)
你可以使用Range.Find的“After”参数继续前进,直到你匹配你想要的Interior.Color。
我做了一个小片段来证明我的意思:
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
答案 1 :(得分:0)
我的解决方案是一个与常规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
似乎可以使用我测试的内容,但您的里程可能会有所不同。
答案 2 :(得分:0)
这是另一种方法。这个使用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
希望这有帮助!
答案 3 :(得分:0)
考虑:
Application.FindFormat.Interior.ColorIndex = 0
rowfound = Range("A:A").Find("123", SearchFormat:=True)