简体   繁体   中英

How to add data from workbook1 into workbook2 and workbook2 updates when workbook1 updates?

I am pretty new to VBA/Macros and I want to create a dashboard that collaborates multiple charts from workbook1 into a new, centralized, worksheet in workbook2. To do this, I decided to add a code into VBA that copies the data from workbook1 into workbook2 however when I add new information into workbook1 that changes its data, it does not auto-update the data in workbook2. Is there a way to sync the sheets and both workbooks so that workbook2 also update without me having to do anything to it? Plus I am trying to turn the data in workbook2 into a chart that updates automatically so that I have a easy to read sheet in workbook2 (basically a dashboard.)

The script below will copy all charts in your workbook to one single sheet. This is a lot cleaner, I think, than moving everything into another Workbook. Hope this helps.

Sub PullOverAllCharts()

    Dim wsChart As Worksheet
    Dim ws As Worksheet
    Dim oChartObj As ChartObject
    Dim oChart As Chart
    Dim NextRow As Long
    Dim NextColumn As Long
    Dim ChartCount As Long


    Set wsChart = ThisWorkbook.Worksheets("Chart Sheet")

    wsChart.Cells.ClearContents

    On Error Resume Next
    wsChart.ChartObjects.Delete
    On Error GoTo 0

    Worksheets("Chart Sheet").Select

    NextRow = 1
    NextColumn = 1
    ChartCount = 0
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Log Sheet" And ws.Name <> "All Data" And ws.Name <> "Chart Sheet" Then
            If ws.ChartObjects.Count > 0 Then
                ChartCount = ChartCount + 1
                ws.ChartObjects(1).Copy
                With wsChart

                    .Paste
                    Set oChartObj = .ChartObjects(.ChartObjects.Count)
                    oChartObj.Left = .Cells(NextRow, NextColumn).Left
                    oChartObj.Top = .Cells(NextRow, NextColumn).Top
                End With
                If ChartCount Mod 4 = 0 Then
                    NextRow = NextRow + 16
                    NextColumn = 1
                Else
                    NextColumn = NextColumn + 8
                End If
            End If
        End If
    Next

    wsChart.Activate


End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM