代码之家  ›  专栏  ›  技术社区  ›  Santhana Krishnan Prakash

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

  •  0
  • Santhana Krishnan Prakash  · 技术社区  · 1 年前

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

    在代码的第29行,我在将代码中的变量p、q、r替换为83104293时遇到了问题。代码如下

    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
    
    1 回复  |  直到 1 年前
        1
  •  2
  •   rotabor    1 年前

    您可以使用+运算符连接字符串。直接使用数字变量,它们将根据默认规则转换为字符串。

    ActiveChart.SetSourceData Source:=Range("BND1!$A$2:$EZ$2,BND1!$A$" + p + ":$EZ$" + p + ",BND1!$A$" + q + ":$EZ$" + q + ",BND1!$A$" + r + ":$EZ$" + r + "")