自动更新工作簿的其他Excel表中的数据 [英] Automatically Update Data in Other Excel Sheets of a Workbook

查看:100
本文介绍了自动更新工作簿的其他Excel表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有VBA代码将我的数据存储在主工作表上,并将其放在工作簿中的其他工作表中。我遇到的问题是新数据不会自动更新。我想开发将自动更新我的工作表的代码。这是我现在的代码。

  Sub test()
Dim LR As Long,i As Long
LR = Range(A & Rows.Count).End(xlUp).Row
对于i = 2到LR
如果Range(B& i).Value =APThen Rows(i)。复制目标:= Sheets(AP)。Range(A& Rows.Count).End(xlUp).Offset(1)
如果Range(B& i).Value = * APThen Rows(i).Copy Destination:= Sheets(If Range(B& i).Value =CSWThen Rows(i).Copy Destination:= Sheets(CSW)。Range (A& Rows.Count).End(xlUp).Offset(1)
如果Range(B& i).Value =COthen Rows(i).Copy Destination:=表格(CO)。范围(A& Rows.Count).End(xlUp).Offset(1)
如果Range(B& i).Value =PSR然后行(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被认为是标题行。一旦您将工作簿设置为下面的代码,您在主表上所做的任何更改将会引发workheet_change事件,导致工作簿中的所有目标工作表都使用最新的数据进行更新。



按照以下步骤使其正常工作:



在主表的代码模块中添加以下内容:



_

  Option Explicit 

Private Sub Worksheet_Change (ByVal Target As Range)
调用UpdateFromMaster
End Sub

添加这些子成为标准模块:



_

  Sub UpdateFromMaster()
'清除您以前写到目的地单上的任何东西
调用ResetDestinationSheets

'您已经有
的代码Dim LR As Long,i As Long
LR = Range(A& Rows.Count).End(xlUp).Row
For i = 2 To LR
如果Range(B& i) .Value =AP然后行(i).Copy Destination:= Sheets(AP)。Range(A& Rows.Count).End(xlUp).Offset(1)
如果Range(B& i).Value =* AP然后行(i).Copy Destination:= Sheets ).Range(A& Rows.Count).End(xlUp).Offset(1)
如果Range(B& i).Value =CSWThen Rows(i)。复制目标:=表(CSW)。范围(A& Rows.Count).End(xlUp).Offset(1)
如果Range(B& i).Value = CO然后行(i).Copy Destination:= Sheets(CO)。Range(A& Rows.Count).End(xlUp).Offset(1)
如果Range(B & i).Value =PSRThen Rows(i).Copy Destination:= Sheets(PSR)。Range(A& Rows.Count).End(xlUp).Offset(1)
Next i

End Sub

_

  Sub ResetDestinationSheets()
'==不优雅,但会在您的示例中工作

调用ResetThisSheet( AP)
调用ResetThisSheet(ALL AP)'我不知道你所谓的这张表
调用ResetThisSheet(CSW)
调用ResetThisSheet(CO)
调用ResetThisSheet(PS R)

End Sub

_

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


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.

解决方案

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

这篇关于自动更新工作簿的其他Excel表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆