Automatically Update Data in Other Excel Sheets of a Workbook

蹲街弑〆低调 提交于 2020-01-02 00:32:10

问题


I have VBA code that takes my data on the "master" worksheet and puts it in the other sheets in a workbook. The problem I am having is that the new data doesn't update automatically. I would like to develop code that will automatically update my worksheets. This is the code that I have now.

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

This puts the data in the other sheets, but when I input new data into the "master" worksheet, the data does not update in the other sheets. I've tried other methods to include auto filter, but they haven't worked.


回答1:


Use the worksheet_change event in your "master" spreadsheet. When data is updated in the "master" sheet, it will raise the worksheet_change event and you can call your code to update the other sheets.

You can find detailed instructions on how to use it here: http://www.ozgrid.com/VBA/run-macros-change.htm

I set up a working example with your code. The workbook has 6 sheets ("master", "AP", "All AP", "CSW", "CO", and "PSR"). Row 1 in each sheet is assumed to be a header row. Once you have your workbook set up with the code below, any changes you make on the "master" sheet will raise the worksheet_change event, causing all of the destination sheets in the workbook to get updated with the most current data.

Follow these steps to get it to work:

Add the following in the code module of the master sheet:

_

Option Explicit

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

Add these subs into a standard module:

_

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



回答2:


To provide an alternative to the worksheet_change() event, which will trigger the code every time a change is made to the worksheet, which you may or may not want. You can also create a shape (or button) and assign your code to that button, so it only runs when you (or the user) tells it to. The advantage of this over the worksheet change() event is that the code won't fire with every little change to the worksheet, which as I stated above, may or may not be desirable.

To assign a macro to a button or shape, add the shape to your sheet, and then right-click and choose Assign Macro. Also see here... How Can I Tie a VBA Macro To A Button In Excel



来源:https://stackoverflow.com/questions/10724420/automatically-update-data-in-other-excel-sheets-of-a-workbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!