macros:loggingmacros不起作用

我使用Record Macro option在Excel中logging了一个macros。 但是,当我运行它,我得到Error 438和错误框显示Object does not support property or method 。 这是生成的macros代码。 有人可以在这里解释:

 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 

当我debugging时,我得到Selection.Cut的错误。 我想知道如何loggingmacros生成不工作的代码

虽然不是一个好方法,

但改变Selection.CutSelection.Parent.Parent.Cut应该工作。

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

稍微磨光的版本(使用引用和避免select)应该工作:

 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