excel - Charts.Add 方法使用 FormulaR1C1 导致意外结果

标签 excel vba

我正在编写一个脚本来解析数据文件,对数据进行一些分析,并显示一些图表;我遇到了这个怪癖。我在 Excel 2010 (Microsoft Office Professional Plus) 中使用 VBA7。

花了很多时间寻找,但似乎在调用 Charts.Add 方法后出现了问题。在该调用之后分配给单元格的 FormulaR1C1 不会产生我期望的结果。在下面的代码中,我期望 =R[0]C[-1] - R[-1]C[-1]在单元格 E3 中被翻译为 =D3-D2 .相反,我得到 =XFD1 - XFD1048576 .

删除新创建的图表会恢复预期的行为。这不是一个理想的解决方案。

Option Explicit
Sub main()

Dim x As Double
Dim ch As Chart

With ThisWorkbook.Sheets("Sheet1")
    'some data to work on
    .Cells(1, 1).Value = "Charts.Add free"
    .Cells(1, 4).Value = "Charts.Add called"
    .Cells(1, 7).Value = "Chart.Delete called"
    For x = 2 To 10
        .Cells(x, 1).Value = Sin(x)
        .Cells(x, 4).Value = Sin(x)
        .Cells(x, 7).Value = Sin(x)
    Next x

    'Desired behavior
    .Cells(3, 2).FormulaR1C1 = "=R[0]C[-1] - R[-1]C[-1]"
    .Cells(3, 2).AutoFill Destination:=.Range(.Cells(3, 2), .Cells(10, 2))

    'after Charts.Add is called, undesired behavior
    Set ch = ThisWorkbook.Charts.Add
    .Cells(3, 5).FormulaR1C1 = "=R[0]C[-1] - R[-1]C[-1]"
    .Cells(3, 5).AutoFill Destination:=.Range(.Cells(3, 5), .Cells(10, 5))

    'after that chart is deleted, desired behavior resumes
    ch.Delete
    .Cells(3, 8).FormulaR1C1 = "=R[0]C[-1] - R[-1]C[-1]"
    .Cells(3, 8).AutoFill Destination:=.Range(.Cells(3, 8), .Cells(10, 8))
End With
End Sub

我能够用上面的代码重现这个怪癖。 AutoFill 语句对于演示不良行为并不是真正必要的。但它们说明了为什么我要使用相对引用而不是绝对引用。

我想我可以通过使用 A1 引用样式来解决我的数据解析脚本中的这个错误。但是,使用 R1C1 比将列字母转换为数字更容易(也更易读)。

另一种解决方法是简单地等到数据解析和分析循环结束来创建图表。但随后我将不得不做额外的记录保存(因为我需要在第二个循环中使用来自第一个循环的信息)。

这只是 VBA 的一个怪癖还是我在这里误解了什么?我当然承认我对 R1C1 很陌生,但为什么图表与 excel 如何将 R1C1 转换为 A1 引用有任何关系?在使用 Charts.Add(除了 Chart.Delete!)之后,我应该做某种“清理”吗?或者也许是 Charts.Add 的替代品?

最佳答案

添加 .Select Set ch = ThisWorkbook.Charts.Add 之间的声明和 .Cells(3, 5).FormulaR1C1 = "=R[0]C[-1] - R[-1]C[-1]" .添加新图表时,它会创建一个新选项卡,然后是事件工作表。要重新激活 Sheet1,您需要 .Select
请参阅下面的新代码。

Option Explicit
Sub main()

Dim x As Double
Dim ch As Chart

With ThisWorkbook.Sheets("Sheet1")
    'some data to work on
    .Cells(1, 1).Value = "Charts.Add free"
    .Cells(1, 4).Value = "Charts.Add called"
    .Cells(1, 7).Value = "Chart.Delete called"
    For x = 2 To 10
        .Cells(x, 1).Value = Sin(x)
        .Cells(x, 4).Value = Sin(x)
        .Cells(x, 7).Value = Sin(x)
    Next x

    'Desired behavior
    .Cells(3, 2).FormulaR1C1 = "=R[0]C[-1] - R[-1]C[-1]"
    .Cells(3, 2).AutoFill Destination:=.Range(.Cells(3, 2), .Cells(10, 2))

    'after Charts.Add is called, undesired behavior
    Set ch = ThisWorkbook.Charts.Add
    .Select   'This statement re-activates 'Sheet1'
    .Cells(3, 5).FormulaR1C1 = "=R[0]C[-1] - R[-1]C[-1]"
    .Cells(3, 5).AutoFill Destination:=.Range(.Cells(3, 5), .Cells(10, 5))

    'after that chart is deleted, desired behavior resumes
    ch.Delete
    .Cells(3, 8).FormulaR1C1 = "=R[0]C[-1] - R[-1]C[-1]"
    .Cells(3, 8).AutoFill Destination:=.Range(.Cells(3, 8), .Cells(10, 8))
End With
End Sub

关于excel - Charts.Add 方法使用 FormulaR1C1 导致意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21302811/

相关文章:

excel - 使用 Err.Raise 重新抛出错误

regex - 匹配字符串vba excel中的日期模式

VBA:获取运行时 1004:使用单元格时对象 'Range' 的方法 '_Worksheet' 失败

vba - 以编程方式保存 Excel 加载项

python - 在 Excel VBA 或 Python 中将 xlsx 文件转换为文件夹和子文件夹内的 xls

vba - Excel VBA-是否可以在发生错误时调用sub?

Excel VBScript 插入列并填充

vba - 根据两个不同的 ID 合并行

vba - 更改 map 颜色的宏(州)

excel - 编译错误: Sub or function not defined (MRound)