我使用VBA时很新,特别是在Excel上格式化图表但已遇到问题。我正在尝试生成图表,但参考单元格的行将根据可用数据而变化。
现在,我的数据在AE741:AG762范围内,但在下一次迭代中,行可能不同。你能帮我写一个动态调整图表数据范围的代码,具体取决于我的数据吗?我手头有的是行引用(即每次迭代我都知道数据的开始和结束位置,在本例中为741到762)。我还在另一个单元格中报告了范围地址,因此在我的情况下,单元格Y10取值AE741:AG762。
我想我唯一需要(但没有做到的)就是将这个范围值调用到我的VBA函数中。
Chart_Test Macro
Set myRange = Workbook().Worksheet(3).Cell(25, 10).Value
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range("AE741:AG762")
ActiveChart.SeriesCollection(2).Select
ActiveChart.SeriesCollection(2).AxisGroup = 2
ActiveChart.SeriesCollection(2).Select
ActiveChart.Legend.Select
Selection.Delete
ActiveChart.Axes(xlCategory).Select
ActiveChart.SeriesCollection(2).Select
ActiveChart.SeriesCollection(2).ChartType = xlColumnClustered
ActiveChart.SeriesCollection(2).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent6
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.25
.Transparency = 0
.Solid
End With
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 240)
.Transparency = 0
End With
End Sub
感谢非常感谢的帮助! 本杰明
答案 0 :(得分:0)
更改这些行:
Set myRange = Workbook().Worksheet(3).Cell(25, 10)
ActiveChart.SetSourceData Source:=Range("AE741:AG762")
对于这些:
Dim myRange As Range 'myRange must be declared as Range
'Ensure that "ThisWorkbook" is alright for you, meaning that you are running this code
'from the Workbook containing the value you want at cell(25,10)
Set myRange = ThisWorkbook.Worksheets(3).Cell(25, 10) '.Cell(25, 10) Or .Range("Y10")
ActiveChart.SetSourceData Source:=Range(myRange.Address)
'Or simply
ActiveChart.SetSourceData Source:=myRange