2017-08-30 22 views
0

我正在尝试使用匹配来查找单元格的行。我已经能够与使用匹配功能和颜色标准

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) 

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

+1

不知道是什么让你觉得你可以使用匹配这样的。尽管可以使用格式化查找方法。 – SJR

+0

使用Range.Find与SearchFormat参数一起查找相关的单元格 – Tragamor

回答

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 
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 

似乎与合作我测试过它,但你的里程可能会有所不同。

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 

希望这会有所帮助!

0

考虑:

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