运行时错误'1004':select范围类失败VBA 2010的方法

我正在试图在表单中填充公式。 我使用的代码是一个录制的macros,它工作正常,直到我连接到一个button。 当我这样做,它给了一个

“运行时错误1004”:select范围类的方法失败“

这是代码,我可以看到没有错。 当我点击debugging时,突出显示第二行

Private Sub CommandButton2_Click() Sheets("DB2 Totbel").Select Range("B2:D2").Select Selection.AutoFill Destination:=Range("B2:D15000"), Type:=xlFillDefault Range("B2:D15000").Select 

VBA中的代码图片

在这里输入图像说明

对于B2:D2和B2:D15000范围的父工作表,存在一些不明确的地方。

 Private Sub CommandButton2_Click() Application.Calculation = xlManual With Sheets("DB2 Totbel") .Range("B2:D2").AutoFill Destination:=.Range("B2:D15000"), Type:=xlFillDefault 'uncomment these is you need to activate the DB2 Totbel worksheet 'and select B2:D15000 '.Activate '.Range("B2:D15000").Select End With Application.Calculation = xlCalculationAutomatic End Sub 

与Range.FillDown方法交替使用。

 Private Sub CommandButton2_Click() Application.Calculation = xlManual With Sheets("DB2 Totbel") .Range("B2:D15000").FillDown End With Application.Calculation = xlCalculationAutomatic End Sub 

logging的macros非常冗长,使用.Select.Activate命令的用法太多。 请参阅如何避免使用Excel中的selectVBAmacros来获取更多的方法来摆脱依靠select和activate来实现您的目标。

  Private Sub CommandButton2_Click() Application.Calculation = xlCalculationManual With Sheets("DB2 Totbel") .Range("B2:D2").AutoFill Destination:=.Range("B2:D15000"), Type:=xlFillDefault End With End Sub