2016-09-27 20 views
0

我想問一些關於以下問題的幫助: 我有兩張工作表。工作表2包含2個帶短語的列表,工作表1有一些數據。我想創建一個宏來檢查Sheet 1中的數據,如果它發現一個沒有出現在Sheet 2列表中的短語,它會給出一個彈出消息。此外,工作表1將包含用逗號分隔的短語。是否也可以檢查? (見截圖) 雖然很簡單,但我已經制定了以下內容,但我相信答案會更復雜。 任何幫助表示讚賞! 提前謝謝!Excel VBA - 通過列表和彈出消息搜索

Sheet 1 with data

Sheet 2 with lists of phrases

我的代碼:

Sub check() 
For i = 2 To 2 

If Cells(i, "A") <> Worksheets("Sheet2").Cells(i, "B") Then 
MsgBox "Phrase 1 does not match" 
    End If 

For j = 2 To 2 
    If Cells(j, "B") <> Worksheets("Sheet2").Cells(j, "C") Then 
    MsgBox "Phrase 2 does not match" 

End If 
Next j 
Next i 
End Sub 
+0

爲什麼你只能通過一個創建一個循環 「循環」細胞? (2至2) – vacip

+0

@vacip可能只是提供一個小例子,說明該操作正在做什麼?在任何情況下,您最好使用'InStr'來定位文本,因爲您嘗試在單元格中查找數據,而不是完全匹配值 – Dave

回答

0

VBA代碼看起來應該是這樣

For all cells in column 'Phase 1' 
Use the SPLIT() function to split the comma separated values 
and store them into a list of distinct values 

    For all distinct values in the list 
    Use the VLOOKUP function on the 'Phrase list 1' column in sheet2, 
    searching for the current value 

    If vlookup returns a valid result then 
     do nothing 
    else 
     pop up message 
    end if 

    end loop of distinct values 

end loop of cells in column 'Phase 1'