2017-09-14 136 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