2017-04-24 77 views
0

請參閱驗證在一列匹配的一組值的值以下的兩個圖像表1和表2如何使用Excel VBA

表1 enter image description here

表2 enter image description here

我試圖驗證Sheet 1「Color」列中的值,即如果Sheet 1「Type」是car,那麼顏色只能是Black和White.Sheet 2具有哪些類型的規則,哪些顏色可以是有效的。如果汽車類型爲表1中的任何其他顏色,則相鄰列應該表示「顏色不匹配」。

for each cell in (sheet 1 - column color) 
    get type from (sheet 1 - column Type) 
    check if (Sheet 1 - column Type) is in (sheet 2 column Type), 
if not give error in adjacent cell "No type" 
    if (Sheet 1 - column Type) is in (sheet 2 column Type) then 
Check if (Sheet 1 - column color) has the valid color 
if not give error "color not matching" 

任何人都可以告訴我如何做到這一點是在Excel中的VB。

P.S,我知道如何做到這一點使用COUNTIFS,但只是想知道這是如何在Excel VB做

謝謝。

+1

COUNTIFS和條件格式規則應該能夠方便地執行此操作。 – Jeeped

回答

1

你不需要VBA。您可以使用sheet1單元格E2中的簡單公式(並向下拖動):

=IF(COUNTIFS(Sheet2!$A$1:$A$1000,Sheet1!D2,Sheet2!$C$1:$C$1000,Sheet1!A2)=0,"color not matching","color OK")