1

我在 excel 中使用Record Macro option. 但是,当我运行它时,我得到Error 438并显示错误框Object does not support property or method。这是生成的宏代码。有人可以在这里解释一下:

Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveCell.Range("A1:C1").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.SetSourceData Source:=Range("'Sheet2'!$A$4:$C$4")
    ActiveChart.ChartType = xlPie
    Application.CutCopyMode = True
    Selection.Cut
    Sheets("Sheet3").Select
    ActiveSheet.Paste
    Sheets("Sheet2").Select
    ActiveCell.Offset(1, 0).Range("A1").Select

当我调试时,我在Selection.Cut. 我想知道一个录制的宏怎么会生成不工作的代码

4

2 回答 2

0

稍微完善的版本(使用引用并避免选择)应该可以工作:

Sub Macro1()
' Macro1 Macro
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ActiveSheet   ' This is likely 'Sheet 1'
    Dim rng As Range
    Set rng = ActiveCell.Range("A1:C1")
    Dim shp As Shape
    Set shp = ws1.Shapes.AddChart
    shp.Chart.SetSourceData Source:=Range("'Sheet2'!$A$4:$C$4")
    shp.Chart.ChartType = xlPie
    Application.CutCopyMode = True
    shp.Cut
    Sheets("Sheet3").Paste
    Set ws2 = Sheets("Sheet2")
    ws2.Activate
    ActiveCell.Offset(1, 0).Range("A1").Select
End Sub
于 2013-11-11T12:59:54.037 回答
0

虽然不是好办法,

Selection.Cut改为Selection.Parent.Parent.Cut应该工作。

请记住,Select尽可能避免使用

于 2013-11-11T10:11:58.197 回答