2017-08-30 37 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)