繁体   English   中英

VBA Excel 宏错误,预期:列表分隔符或)

[英]VBA Excel Macro Error , Expected: list separator or )

我是 Excel VBA 的新用户,最近我在尝试运行宏时遇到此错误。 我的宏所做的是通过读取单元格行数据并自己创建一个图表用于导出等。

下面是我的宏代码

Sub CombinationChart()

    Dim lastrow As String
    Dim boundaryRow As String
    
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    lastrow = mySheet.Range("A" & Rows.Count).End(xlUp).Row
    boundaryRow = lastrow - 20
    ActiveChart.SetSourceData Source:=Range("mySheet!$A$" & boundaryRow ":$B$" & lastrow)  'make sure the range is correct here
    ActiveChart.FullSeriesCollection(1).ChartType = xlLine 'select which column should be the Line or the Column
    ActiveChart.FullSeriesCollection(1).AxisGroup = 1
End Sub

错误部分在这里

ActiveChart.SetSourceData Source:=Range("mySheet!$A$" & boundaryRow ":$B$" & lastrow)  'make sure the range is correct here

我的最后一行变量是包含 excel 表中的数据的最后一行,而 boundaryRow 是一个变量,它获取最后一行值并将其减去 20,即最后一行 - 20,但我只是找不到一种方法来放入我的将两个变量放入我的 ActiveChart.Range。

您错过了问题行中的连接运算符( & )。

以下是它的外观:

ActiveChart.SetSourceData Source:=Range("mySheet!$A$" & boundaryRow & ":$B$" & lastrow)  

boundaryRow":$B$"之间缺少&

以下是如何使用Range object 变量执行您想要的操作。 我在这里假设 - 因为您使用它来计算lastRow - 您已将mySheet定义为Worksheet object(可能是全局的)。

Sub CombinationChart()

    Dim lastrow As Long
    Dim boundaryRow As Long
    Dim chartData as Range
     
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    lastrow = mySheet.Range("A" & Rows.Count).End(xlUp).Row 'mySheet is defined elsewhere
    boundaryRow = lastrow - 20
    Set chartData = mySheet.Cells(boundaryRow,1).Resize(21,2) ' 21 rows x 2 columns

    ActiveChart.SetSourceData Source:=chartData  'make sure the range is correct here
    ActiveChart.FullSeriesCollection(1).ChartType = xlLine 'select which column should be the Line or the Column
    ActiveChart.FullSeriesCollection(1).AxisGroup = 1
End Sub

您也可以“即时”执行此操作,将我分配给chartData的表达式替换为SetSourceData Source参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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