2017-09-14 125 views
2

嗨,那裏我很新的VBA和任何方向將不勝感激。Excel VBA:範圍與條件匹配

我已經開始編寫一個函數來檢查表中輸入的ID是否已存在。 到目前爲止,我認爲我有一個函數可以比較一系列值(ID),並在值顯示消息框中突出顯示(如果它已經存在)。

我想添加的是這兩個條件被標記之前。所以....

所以基本上

  • 步驟1:我想匹配從範圍 「B」 列的值到另一個範圍時, 「匹配」(片材)柱B
  • 步驟2:然後如果一個字段的值"" = "Match".column "E""f" = "Match".column "F"
  • 第3步:然後通知/亮點

希望是有道理的.....掙扎繼續摹想拉相應的列E/F從原來的範圍值相匹配

大加讚賞, 克里斯

Sub RefCheck() 
    Dim sh As Worksheet, 
    lr As Long, 
    c As Range 

    Set sh = Sheets("sheet1") 'Upload sheet name 
    Set st = Sheets("sheet2") 'DB data Edit sheet name 

    lr = st.Cells(Rows.Count, "B").End(xlUp).Row 

    For Each c In sh.Range("B7:B" & sh.Cells(Rows.Count, "B").End(xlUp).Row) 
     Set dval = st.Range("B2:B" & lr).Find(c.Value, LookIn:=xlValues, LookAt:=xlWhole) 

     'Add in additional matching criteria here 

     'Flag values in range that exists in DB Range (above), if the "ID" value in upload sheet Column "E" ='s the "ID" in DB data sheet Column "E" 
     'or "Name" in upload sheet Column "F" ='s the "Name" Column "F" in the DB sheet THEN colour and msgbox 


     If Not daval Is Nothing Then 
      c.Interior.ColorIndex = 3 
      my_Alarm = MsgBox("Reference already exists, B" & c.Row, vbExclamation, "Validation Error") 
     End If 

    Next 
End Sub 

回答

1

我不知道這是不是你的問題,但你測試DAVAL和設置以前的dval

If Not dval Is Nothing Then 
     c.Interior.ColorIndex = 3 
     my_Alarm = MsgBox("Well Reference already exists, B" & c.Row, vbExclamation, "Validation Error") 
    End If 
+0

Option Explicit如何幫助避免浪費時間排除錯別字的一個很好的例子! – KacireeSoftware