我希望你一切都好
我有一張表,其中有專案團隊的成員。它包含兩行:第一行用于 ID,第二行用于名稱

當作業表被激活時,團隊成員會從資料庫中加載,包括 ID 和名稱。
當我想在團隊中添加新成員時,流程如下:
- 在第一行手動寫入要添加的成員的 id
- 該作業表根據在步驟 1 中鍵入的 id 從另一個作業表加載名稱
對于第 2 步,有兩種方法:
- 通過在第二行中撰寫一個公式,對于固定的大量單元格,根據第一行的 id 從另一張表中提取名稱。
- 使用如下
vba代碼處理第一行的更改事件:
private sub Worksheet_change(ByVal target as Range)
if Not Application.Intersect(target.row, ActiveSheet.rows(1)) Is Nothing then
Insert new column 'It is important, without this the changes are not applicable
look for the name of the id ih the target cell and make changes
End If
第二種解決方案更加動態,我更喜歡這種方式,但是我遇到了一個問題:在執行第 1 步(從資料庫加載 ID)時也會應用此子程式,使vba程式無休止地運行,因此阻止了 excel
問題是 :
有什么方法可以處理與編程更改不同的用戶更改vba?
預先感謝
uj5u.com熱心網友回復:
您要確保由 VBA 引起的更改不會觸發另一個Worksheet_change事件。無需檢查原始事件是由人還是程式引起的。您需要做的就是防止無限回圈。每次使用Worksheet_change活動時都需要牢記這一點。
您可以通過首先在Worksheet_change事件開始時禁用事件,然后在結束時重新啟用事件來做到這一點。
Private sub Worksheet_change(ByVal target as Range)
Application.EnableEvents = False ' Disable events to prevent infinite loop
If Not Application.Intersect(target.row, ActiveSheet.rows(1)) Is Nothing then
Insert new column 'It is important, without this the changes are not applicable
look for the name of the id in the target cell and make changes
End If
Application.EnableEvents = True ' Re-enable events
End Sub
因此,每當有人更改作業表中的某些內容時,就會觸發該事件。一旦事件開始,事件就會被禁用,這會阻止 VBA 自行觸發。在事件結束時,事件將重新啟用,以便它可以回應另一個用戶引起的更改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407573.html
標籤:
