如果只有一行VBA代码应用自动填充方法

下面的代码片段的自动填充方法工作正常,如果有超过1行但失败,并提供运行时错误1004的情况下,只有一行,请build议所需的更改。

Lastrow = ws3.Cells(Rows.Count, "B").End(xlUp).Row ws1.Range("A2").FormulaR1C1 = _ "=TEXT(RC[6],""000000000000"")&""-""&TEXT(RC[7],""0000"")" ws1.Range("A2").AutoFill Destination:=ws1.Range("A2:A" & Lastrow), Type:=xlFillDefault 

这是一个非常快速的修复:

 lastrow = Cells(Rows.Count, "B").End(xlUp).Row Range("A2").FormulaR1C1 = "=TEXT(RC[6],""000000000000"")&""-""&TEXT(RC[7],""0000"")" If lastrow > 2 Then Range("A2").AutoFill Destination:=Range("A2:A" & lastrow), Type:=xlFillDefault End If 

一般来说,它会工作。 它给你一个错误,因为它的lastrow是1 ,它不能用它做AutoFill