2017-02-23 31 views
1

嘿我想知道是否可行:目前我有工作表Sheet1 一個下拉列表包含使用數據驗證從Sheet2中被拉到名稱的列表。 該下拉列表旁邊的單元格使用來自sheet2的VLOOKUP填充電話號碼。
我的問題:我可以使用VBA,以便每次在下拉列表中都有變化時,它旁邊的單元格將從數據填充到SHEET2?請記住,選擇的值需要拉出正確的電話號碼。VBA下降downlist平變化

爲什麼我問我的文件是否有效?因爲任何人都會意外刪除VLOOKUP公式,所以我無法保護它,因爲它是一個使用多個不同宏的共享文檔。

回答

1

像這樣的東西? (來源:https://support.microsoft.com/en-us/help/213612/how-to-run-a-macro-when-certain-cells-change-in-excel

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim KeyCells As Range 

    ' The variable KeyCells contains the cells that will 
    ' cause an alert when they are changed. 
    Set KeyCells = Range("A1") 

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _ 
     Is Nothing Then 

    ' Display a message when one of the designated cells has been 
    ' changed. 
    ' Place your code here. 
    Range("B1").Formula = "=VLookup(A1,LookupTable,2,FALSE)" 

    End If 
End Sub