单元格引用自动填充错误1004对象定义的错误

时间:2015-12-01 03:34:10

标签: excel excel-vba vba

我尝试自动填充一组根据用户输入而变化的列。但是,当我引用范围的单元格时,它会给我一个错误。错误出现在最后一行

Dim stocknum As Integer
stocknum = Sheets("Sheet1").Cells(1, 1)

Dim a As Integer
Dim j As Integer
Dim k As Integer
Dim LastRow1 As Long

Dim LastRow() As String
ReDim LastRow(0 To stocknum + 1) As String
Dim LastCol As Integer


j = stocknum + 2
k = stocknum + 5
l = stocknum + stocknum + 5

Worksheets("Sheet1").Activate
LastRow1 = (Range("B9").End(xlDown).Row) - 7
LastCol = (Range("B9").End(xlToRight).Column) - 1


Worksheets("stock_returns").Activate
Sheets("stock_returns").Cells(3, 2).Formula = "=('Sheet1'!B9-'Sheet1'!B10)/'Sheet1'!B10"
Sheets("stock_returns").Cells(3, 2).Select
Selection.AutoFill Destination:=Sheets("stock_returns").Range("Cells(3, 2), Cells(3, LastCol)")

1 个答案:

答案 0 :(得分:0)

尝试以下方法:你在最后一行犯了错误。只需从Cells()函数部分删除双引号("")。

     Dim stocknum As Integer
        stocknum = Sheets("Sheet1").Cells(1, 1)

        Dim a As Integer
        Dim j As Integer
        Dim k As Integer
        Dim LastRow1 As Long

        Dim LastRow() As String
        ReDim LastRow(0 To stocknum + 1) As String
        Dim LastCol As Integer


        j = stocknum + 2
        k = stocknum + 5
        l = stocknum + stocknum + 5

        Worksheets("Sheet1").Activate
        LastRow1 = (Range("B9").End(xlDown).Row) - 7
        LastCol = (Range("B9").End(xlToRight).Column) - 1


        Worksheets("stock_returns").Activate
        Sheets("stock_returns").Cells(3, 2).Formula = "=('Sheet1'!B9-'Sheet1'!B10)/'Sheet1'!B10"
        Sheets("stock_returns").Cells(3, 2).Select
        Selection.AutoFill Destination:=Sheets("stock_returns").Range(Cells(3, 2), Cells(3, LastCol))