Excel 2003:VBA语法在Excel 2010中导致问题 – 替代方法?

我有一个旧的VBA脚本(Office 2003)来解决在Office 2010中失败的问题。这个问题似乎是由用于引用工作表和单元格的语法造成的。

ActiveChart.SeriesCollection(1).XValues = _ "=EP!Z1S2:Z1S" & Mid(Str(Schritte + 2), 2) With ActiveChart.SeriesCollection(1) .Values = "=EP!Z3S2:Z3S" & Mid(Str(Schritte + 2), 2) .Name = "=EP!Z3S1" End With 

是否有替代使用“= Sheet!CellRange”? 或者可以通过更改Office / Excel中的某些configuration来解决问题?

您需要提供单元格范围作为编码的Range对象和/或Range.Cells属性 ,可能还有Range.Resize属性 。

 dim ws as worksheet set ws = worksheets("EP") ActiveChart.SeriesCollection(1).XValues = _ ws.range("Z1S2:Z1S" & Mid(Str(Schritte + 2), 2)) With ActiveChart.SeriesCollection(1) .Values = ws.cells(3, 2).resize(1, int(Mid(Str(Schritte + 2), 2))) '=EP!Z3S2:Z3S" & Mid(Str(Schritte + 2), 2) .Name = ws.cells(3, 1) '=EP!Z3S1 End With 

当我使用严格描述范围的string创build图表时,我收到编译错误:types不匹配 。 只要我在Range包装string或直接使用.Cells引用它们,问题就消失了,并生成了图表。