我正在使用两个Excel 2013工作簿标签:“指标”和“图表日期”。度量工作表包含八个图表以及通过其他工作表提供给它的数据。图表数据工作表包含从“度量标准”选项卡接收的数据行(每周连续一行)。我的问题是,我正在试图弄清楚如何在图表数据工作表中获取数据行并运行一个宏,每周更新一次(比如下周更改32到33,然后下周更新34,以及所以,每次更新Metrics选项卡时。我还需要能够更新图表。这是我目前的代码:
Sub UpdateChartData()
'Appends the new data from the "Metrics" tab of workbook to the Chart Data tab, appending a new row for each week.
Sheets("Chart Data").Select
Range("A32").FormulaR1C1 = "=R[-1]C+7" 'Adds 7 days to the previous week's date.
Range("B32").FormulaR1C1 = "=Metrics!R[-30]C[1]"
Range("C32").FormulaR1C1 = "=Metrics!R[-29]C"
Range("D32").FormulaR1C1 = "=Metrics!R[-18]C[-1]"
Range("E32").FormulaR1C1 = "=Metrics!R[-8]C[-2]"
Range("H32").FormulaR1C1 = "=R[-1]C+7" 'Adds 7 days to the previous week's date.
Range("J32").FormulaR1C1 = "=Metrics!R[-29]C[-6]"
Range("K32").FormulaR1C1 = "=Metrics!R[-18]C[-7]"
Range("L32").FormulaR1C1 = "=Metrics!R[-8]C[-8]"
Range("N32").FormulaR1C1 = "=R[-1]C+7"'Adds 7 days to the previous week's date.
Range("P32").FormulaR1C1 = "=Metrics!R[-30]C[-11]"
Range("Q32").FormulaR1C1 = "=Metrics!R[-18]C[-12]"
Range("R32").FormulaR1C1 = "=Metrics!R[-8]C[-13]"
Range("T32").FormulaR1C1 = "=R[-1]C+7" 'Adds 7 days to the previous week's date.
Range("U32").FormulaR1C1 = "=Metrics!R[-30]C[17]"
Range("V32").FormulaR1C1 = "=Metrics!R[-29]C[16]"
Range("W32").FormulaR1C1 = "=Metrics!R[-18]C[15]"
Range("X32").FormulaR1C1 = "=Metrics!R[-8]C[14]"
Range("AA32").FormulaR1C1 = "=R[-1]C+7" 'Adds 7 days to the previous week's date.
Range("AB32").FormulaR1C1 = "=Metrics!R[-30]C[13]"
Range("AC32").FormulaR1C1 = "=Metrics!R[-29]C[12]"
Range("AD32").FormulaR1C1 = "=Metrics!R[-18]C[11]"
Range("AE32").FormulaR1C1 = "=Metrics!R[-8]C[10]"
Range("AH32").FormulaR1C1 = "=R[-1]C+7" ' Adds 7 days to the previous week's date.
Range("AI32").FormulaR1C1 = "=Metrics!R[-30]C[-25]"
Range("AJ32").FormulaR1C1 = "=Metrics!R[-29]C[-26]"
Range("AK32").FormulaR1C1 = "=Metrics!R[-18]C[-27]"
Range("AL32").FormulaR1C1 = "=Metrics!R[-8]C[-28]"
Range("AO32").FormulaR1C1 = "=R[-1]C+7" 'Adds 7 days to the previous week's date.
Range("AP32").FormulaR1C1 = "=Metrics!R[-30]C[-15]"
Range("AQ32").FormulaR1C1 = "=Metrics!R[-29]C[-16]"
Range("AR32").FormulaR1C1 = "=Metrics!R[-18]C[-17]"
Range("AS32").FormulaR1C1 = "=Metrics!R[-8]C[-18]"
Range("AV32").FormulaR1C1 = "=R[-1]C+7" 'Adds 7 days to the previous week's date.
Range("AW32").FormulaR1C1 = "=Metrics!R[-30]C[-24]"
Range("AX32").FormulaR1C1 = "=Metrics!R[-29]C[-25]"
Range("AY32").FormulaR1C1 = "=Metrics!R[-18]C[-26]"
Range("AZ32").FormulaR1C1 = "=Metrics!R[-8]C[-27]"
UpdateCharts
End Sub
“############################################## ##############################
Sub UpdateCharts()
' Updates the charts based on the data that was updated in the "Chart Data" tab for given week.
Sheets("Metrics").Select
ActiveSheet.ChartObjects("Chart 8").Activate
ActiveChart.SetSourceData Source:=Sheets("Chart Data").Range("A1:F32")
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SetSourceData Source:=Sheets("Chart Data").Range("H2:L32")
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.SetSourceData Source:=Sheets("Chart Data").Range("N2:R32")
ActiveSheet.ChartObjects("Chart 3").Activate
ActiveChart.SetSourceData Source:=Sheets("Chart Data").Range("T2:Y32")
ActiveSheet.ChartObjects("Chart 7").Activate
ActiveChart.SetSourceData Source:=Sheets("Chart Data").Range("AV2:BA32")
ActiveSheet.ChartObjects("Chart 6").Activate
ActiveChart.SetSourceData Source:=Sheets("Chart Data").Range("AO2:AT32")
ActiveSheet.ChartObjects("Chart 5").Activate
ActiveChart.SetSourceData Source:=Sheets("Chart Data").Range("AH2:AM32")
ActiveSheet.ChartObjects("Chart 4").Activate
ActiveChart.SetSourceData Source:=Sheets("Chart Data").Range("AA2:AF32")
End Sub
任何建议都会非常感激。感谢。
答案 0 :(得分:1)
未经测试,但这可以帮助您入门:
Sub UpdateChartData()
' Appends the new data from the "Metrics" tab of workbook
' to the Chart Data tab, appending a new row for each week.
Dim r As Range
'find the first empty row in ColA (working up from the bottom of the sheet)
Set r = Sheets("Chart Data").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
With r.EntireRow
'here we're using .Range("A1") *relative* to the row
' we're working on: Eg Range("A8").Range("A1") is A8
.Range("A1").FormulaR1C1 = "=R[-1]C+7" 'Adds 7 days
.Range("B1").FormulaR1C1 = "=Metrics!R[-30]C[1]"
.Range("C1").FormulaR1C1 = "=Metrics!R[-29]C"
.Range("D1").FormulaR1C1 = "=Metrics!R[-18]C[-1]"
.Range("E1").FormulaR1C1 = "=Metrics!R[-8]C[-2]"
'............
'etc. etc....
'............
.Range("AY1").FormulaR1C1 = "=Metrics!R[-18]C[-26]"
.Range("AZ1").FormulaR1C1 = "=Metrics!R[-8]C[-27]"
End With
UpdateCharts r.Row 'pass the just-filled row number
End Sub
Sub UpdateCharts(rw As Long)
' Updates the charts based on the data that was updated
' in the "Chart Data" tab for given week. Sheets("Metrics").Select
Dim sht As Worksheet
Set sht = Sheets("Chart Data")
'No need to Activate any charts - you can work directly with them
sht.ChartObjects("Chart 8").Chart.SetSourceData Source:=sht.Range("A1:F" & rw)
sht.ChartObjects("Chart 1").Chart.SetSourceData Source:=sht.Range("H2:L" & rw)
sht.ChartObjects("Chart 2").Chart.SetSourceData Source:=sht.Range("N2:R" & rw)
sht.ChartObjects("Chart 3").Chart.SetSourceData Source:=sht.Range("T2:Y" & rw)
sht.ChartObjects("Chart 7").Chart.SetSourceData Source:=sht.Range("AV2:BA" & rw)
sht.ChartObjects("Chart 6").Chart.SetSourceData Source:=sht.Range("AO2:AT" & rw)
sht.ChartObjects("Chart 5").Chart.SetSourceData Source:=sht.Range("AH2:AM" & rw)
sht.ChartObjects("Chart 4").Chart.SetSourceData Source:=sht.Range("AA2:AF" & rw)
End Sub