簡體   English   中英

自動更新工作簿的其他Excel工作表中的數據

[英]Automatically Update Data in Other Excel Sheets of a Workbook

我有VBA代碼,可將我的數據保存在“主”工作表上,並將其放在工作簿的其他工作表中。 我遇到的問題是新數據不會自動更新。 我想開發將自動更新我的工作表的代碼。 這是我現在擁有的代碼。

Sub test()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
If Range("B" & i).Value = "AP" Then Rows(i).Copy Destination:=Sheets("AP").Range("A" & Rows.Count).End(xlUp).Offset(1)
If Range("B" & i).Value = "*AP" Then Rows(i).Copy Destination:=Sheets(" If Range("B" & i).Value = "CSW" Then Rows(i).Copy Destination:=Sheets("CSW").Range("A" & Rows.Count).End(xlUp).Offset(1)
If Range("B" & i).Value = "CO" Then Rows(i).Copy Destination:=Sheets("CO").Range("A" & Rows.Count).End(xlUp).Offset(1)
If Range("B" & i).Value = "PSR" Then Rows(i).Copy Destination:=Sheets("PSR").Range("A" & Rows.Count).End(xlUp).Offset(1)
Next i
End Sub

這會將數據放在其他工作表中,但是當我在“主”工作表中輸入新數據時,數據不會在其他工作表中更新。 我嘗試了其他方法來包括自動過濾器,但是它們沒有用。

在“主”電子表格中使用worksheet_change事件。 在“主”表中更新數據時,它將引發worksheet_change事件,您可以調用代碼來更新其他表。

您可以在此處找到有關如何使用它的詳細說明: http : //www.ozgrid.com/VBA/run-macros-change.htm

我用您的代碼建立了一個有效的示例。 該工作簿有6頁(“主”,“ AP”,“所有AP”,“ CSW”,“ CO”和“ PSR”)。 假定每張紙的第1行是標題行。 使用下面的代碼設置工作簿后,您在“主”表上進行的任何更改都會引發worksheet_change事件,從而導致工作簿中的所有目標表都使用最新數據進行更新。

請按照以下步驟操作:

在主表的代碼模塊中添加以下內容:

_

Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
        Call UpdateFromMaster
    End Sub

將這些子添加到標准模塊中:

_

Sub UpdateFromMaster()
    ' clear whatever you had previously written to the destination sheets
    Call ResetDestinationSheets

    ' the code you already had
    Dim LR As Long, i As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To LR
    If Range("B" & i).Value = "AP" Then Rows(i).Copy Destination:=Sheets("AP").Range("A" & Rows.Count).End(xlUp).Offset(1)
    If Range("B" & i).Value = "*AP" Then Rows(i).Copy Destination:=Sheets("All AP").Range("A" & Rows.Count).End(xlUp).Offset(1)
    If Range("B" & i).Value = "CSW" Then Rows(i).Copy Destination:=Sheets("CSW").Range("A" & Rows.Count).End(xlUp).Offset(1)
    If Range("B" & i).Value = "CO" Then Rows(i).Copy Destination:=Sheets("CO").Range("A" & Rows.Count).End(xlUp).Offset(1)
    If Range("B" & i).Value = "PSR" Then Rows(i).Copy Destination:=Sheets("PSR").Range("A" & Rows.Count).End(xlUp).Offset(1)
    Next i

End Sub

_

Sub ResetDestinationSheets()
    '== not elegant, but will work in your example

    Call ResetThisSheet("AP")
    Call ResetThisSheet("ALL AP") ' I didn't know what you called this sheet
    Call ResetThisSheet("CSW")
    Call ResetThisSheet("CO")
    Call ResetThisSheet("PSR")

End Sub

_

Sub ResetThisSheet(ByRef SheetToClear As String)
    Sheets(SheetToClear).Range("A2:B" & Rows.Count).Clear
End Sub

提供對worksheet_change()事件的替代方法,該事件將在每次更改工作表時觸發您可能想要或不希望的代碼。 您還可以創建形狀(或按鈕)並將代碼分配給該按鈕,因此僅在您(或用戶)告訴它時才運行。 相對於工作表change()事件,此方法的優點在於,代碼不會隨着工作表的任何微小更改而觸發,如上所述,這可能是可取的,也可能是不希望的。

要將宏分配給按鈕或形狀,請將形狀添加到工作表中,然后右鍵單擊並選擇“分配宏”。 另請參見此處... 如何將VBA宏綁定到Excel中的按鈕

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM