Excel VBA - 在嵌入图表中使用具有多个变量的范围函数时遇到问题

问题描述 投票:0回答:1

使用 VBA 在工作表中,我尝试多次复制和粘贴嵌入图表并为每个新图表分配不同的数据范围。 每个图表在 y 轴上有 3 个数据系列,在 x 轴上有 1 个数据系列

在代码的第29行中,我在将代码中的变量p、q、r分配到83、104、293的位置时遇到问题,代码如下

Sub CopyChart()
Dim Sht As Worksheet
Dim chrt As Chart
Dim n As Integer
Dim k As Integer
Dim p As Integer
Dim q As Integer
Dim r As Integer
Set Sht = ThisWorkbook.Sheets("Sheet2")
Set ShtBND1 = ThisWorkbook.Sheets("BND1")
Set chrt = Sht.ChartObjects("Chart 1").Chart
n = 99
k = 2
p = 83
q = 104
r = 293
chrt.ChartArea.Copy

Do Until k >= 4
Sht.Range("A1").Select
ActiveSheet.Paste
    With ActiveChart.Parent
         .Top = n    ' reposition new chart
         .Left = 180   ' reposition new chart 
         .Name = "Chart " & k
     End With
     
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("BND1!$A$2:$EZ$2,BND1!$A$83:$EZ$83,BND1!$A$104:$EZ$104,BND1!$A$293:$EZ$293")

'I am having issues to assign the variables p, q, r in above code in the place of 83, 104, 293

ActiveChart.PlotBy = xlColumns
ActiveChart.PlotBy = xlRows
    
n = n + 92
k = k + 1
p = p + 1
q = q + 1
r = r + 1
Loop
End Sub
excel vba variables charts range
1个回答
0
投票
ActiveChart.SetSourceData Source:=Range("BND1!$A$2:$EZ$2,BND1!$A$" + p + ":$EZ$" + p + ",BND1!$A$" + q + ":$EZ$" + q + ",BND1!$A$" + r + ":$EZ$" + r + "")
© www.soinside.com 2019 - 2024. All rights reserved.