简体   繁体   English

Excel 2003:VBA语法在Excel 2010中引起问题-替代方法?

[英]Excel 2003: VBA Syntax causes problems in Excel 2010 - Alternatives?

I've got an old VBA-script (Office 2003) to fix which fails in Office 2010. The problem seems to be caused by the syntax used to reference sheets and cells. 我有一个旧的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

Is there an alternative to using "=Sheet!CellRange"? 有没有使用“ = Sheet!CellRange”的替代方法? Or can the problem be solved by changing some configuration in Office / Excel? 还是可以通过更改Office / Excel中的某些配置来解决问题?

You need to provide the cell ranges as coded Range objects and/or Range.Cells property , possibly with the Range.Resize property . 您需要将单元格范围提供为编码的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

When I created a chart using strictly strings describing the ranges, I received Compile error: Type mismatch . 当我使用严格描述范围的字符串创建图表时,收到编译错误:类型不匹配 As soon as I wrapped the strings in Range or referenced them more directly with .Cells the problem disappeared and the chart generated. 一旦将字符串包装在Range或使用.Cells更直接地引用它们,问题就消失了,并生成了图表。

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

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