Excel VBA:范围与条件匹配

您好,我是相当新的VBA和任何方向将不胜感激。

我已经开始编写一个函数来检查表中input的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 

我不知道这是否是你的问题,但是你testingdaval并且设置了以前的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