Error 1004 : autofill method of range class failed vba excel 2010
我正在运行下面的 VBA 代码 -
1 2 3 4 5 6 | lastColumn = Cells(5, Columns.count).End(xlToLeft).Column count_dates = Range("H2").Value Set cellSource = Range(Cells(6, 13).Address) Set cellTarget = Range(Cells(6, 13), Cells(count_dates + 5, lastColumn)) cellSource.AutoFill Destination:=cellTarget, Type:=xlFillDefault |
代码在一台 PC (Excel 2016) 上运行良好,但在另一台 PC 上运行不正常 (Excel 2010)。我无法找出相同的原因。
单元格 (6,13) 有一个公式,我想水平向右和垂直向下拖动
我得到的错误是 -
错误 1004:范围类的自动填充方法失败
在 Excel 2010 中,自动填充仅在一个方向上工作,水平或垂直。因此,如果您希望填充二维范围,您应该首先水平填充一行(包括源单元格),然后将该行用作源将整行向下复制。下面的代码就是这样做的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Sub AutoFillRange() Dim lastColumn As Long Dim count_Dates As Long Dim cellSource As Range, cellTarget As Range lastColumn = Cells(5, Columns.Count).End(xlToLeft).Column count_Dates = Range("H2").Value Set cellSource = ActiveSheet.Cells(6,"M") If lastColumn <> cellSource.Column Then ' return an unexpected result if lastColumn is smaller than cellSource.Column Set cellTarget = cellSource.Resize(1, Abs(lastColumn - cellSource.Column + 1)) cellSource.AutoFill Destination:=cellTarget, Type:=xlFillDefault Set cellSource = cellTarget End If Set cellTarget = cellSource.Resize(count_Dates, cellSource.Columns.Count) cellSource.AutoFill Destination:=cellTarget, Type:=xlFillDefault End Sub |