Excel VBA:范围匹配条件

时间:2017-09-14 08:04:13

标签: excel vba excel-vba

大家好,我对VBA很新,任何方向都会受到赞赏。

我已经开始编写一个函数来检查列中输入的ID是否已存在于表中。 到目前为止,我认为我有一个比较一系列值(ID' s)的函数,并突出显示值/显示消息框(如果它已经存在)。

我想要添加的是在标记之前的两个条件。所以....

所以基本上

  • 第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 个答案:

答案 0 :(得分: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