2014-06-30 80 views
0

所以,我有兩列17 & 18在那裏我有多個行,如:Excel中使用宏循環

17 | 18<br> 
ttt | xxx<br> 
tty | xxy<br> 
eer | eet<br> 
fff | fft<br> 

等... 我想要做的是在開始行2列17什麼,搶TTT然後看看是否在第17或18列再次出現。如果不是,我需要向用戶顯示一條消息,如果確實如此,請說第20行第18列我需要忽略它並標記我已經發現這個價值,並且當我下到那裏時不想再遇到它。

我希望這是有道理的......

我認爲做正確的事就是用一個Do循環,看看是這樣的:

Dim X As Range 
    Do While Cells(X, 17) <> "" 
     Do While Cells(X,18) <> "" 
      Cells.Find(What:=X.Value, After:=activeCell).Active 
     Loop 
    Loop 

有沒有人嘗試過這樣做嗎?

+0

是在R2C17值你所關心的,還是你試圖找到這些列中的所有唯一值的唯一價值? –

回答

1

我不會使用範圍.Find方法。只需使用Application.Match函數或WorksheetFunction.CountIf函數即可。爲了在第二次/後續過程中忽略它,你將需要存儲要在內存中忽略的值列表,我建議爲此使用字典對象。

像這樣的東西也許(未經測試):

Sub foo() 
Dim column1 as Range 
Dim rngToCheck as Range 
Dim r as Range 
Dim dict as Object 


'use a dictionary object to keep track of the items that appear more than once 
Set dict = CreateObject("Scripting.Dictionary") 

Set column1 = Range("A1:A100") 'Modify as needed -- only the first column 

Set rngToCheck = Range("A1:B100") 'Modify as needed -- both columns 

'Check each value in column1 against the entire range 
For each r in column1 
    'ignoring anything that already has been added to the dictionary 
    If not dict.Exists(r.Value) Then 
     If WorksheetFunction.CountIf(rngToCheck, r.Value) > 1 then 
      'if it appears more than once then add it to the dictionary so to ignore 
      ' it the next time the macro encounters this value: 
      dict(r.Value) = dict(r.Value) 
     Else 
      'if this value only appears once, then it doesn't appear anywhere else, _ 
      ' so msgbox to the user. Modify msgbox as needed: 
      MsgBox r 

     End If 
    End If 
Next 

End Sub 
+0

優秀!!謝謝:-),這就是我所想的,只是不知道該怎麼做 – user1772086