将配方应用于一系列细胞

时间:2016-04-20 12:02:51

标签: excel vba excel-vba

我有一些我知道可行的代码,我将SUMIF公式应用于一系列单元格。它可以工作,但它在底部添加了一大堆额外的行,不应该存在。我尝试添加一个do until循环,但它陷入无限循环并崩溃。

这是我的第一批可行的代码,但仅在已复制的列上添加了额外的行。

    Dim z As Workbook   'Budget Workbook
    Dim y As Workbook   'Formatted - current workbook
    Dim lastRow As Integer
    Dim budgLastRow As Integer
    Dim rng As Range

    Set y = Workbooks("DLT.xlsm")
    Set z = Workbooks.Open("C:\Reports\Budget.xlsx")

'Apply function to columns to pull costing data
    With y.Worksheets("DLT")
        lastRow = Cells(Rows.Count, 5).End(xlUp).Row

            For Each rng In .Range("AI22:AI" & lastRow)
                rng.Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E" & rng.Row & ",'[Budget.xlsx]DynamicReport'!H:H)"
                rng.Value = rng.Value
            Next rng

            For Each rng In .Range("AJ22:AJ" & lastRow)
                rng.Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E" & rng.Row & ",'[Budget.xlsx]DynamicReport'!I:I)"
                rng.Value = rng.Value
            Next rng

            For Each rng In .Range("AN22:AN" & lastRow)
                rng.Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E" & rng.Row & ",'[Budget.xlsx]DynamicReport'!E:E)"
                rng.Value = rng.Value
            Next rng

            For Each rng In .Range("AO22:AO" & lastRow)
                rng.Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E" & rng.Row & ",'[Budget.xlsx]DynamicReport'!G:G)"
                rng.Value = rng.Value
            Next rng

    End With

我认为其他附加行已被复制,因为预算工作簿包含的数据比格式化的工作簿更多。我知道可能会删除已经被复制的其他不必要的行。

所以我在

中添加了这一小段代码
    With y.Worksheets("Formatted")
        lastRow = Cells(Rows.Count, 5).End(xlUp).Row - 1
        budgLastRow = Cells(Rows.Count, 35).End(xlUp).Row
        Rows("AI" & lastRow & ":AO" & budgLastRow).EntireRow.Delete
    End With

我在行

上得到了一个应用程序定义的错误对象定义错误
 Rows("AI" & lastRow & ":AO" & budgLastRow).EntireRow.Delete

这可能不是最有效的方法,但这是我能想到的唯一方法。我对VBA相当新,只编了几个月,所以大多只是尝试不同的方式,看看有什么用。任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

您没有正确限定lastRow变量的范围:

lastRow = .Cells(.Rows.Count, 5).End(xlUp).Row

请注意CellsRows之前的点。

还有几点:

始终对行变量使用Long而不是Integer,因为工作表中的行数多于整数可以容纳的行数。

您不需要循环将相同的公式放在单元格列中。

Dim z                     As Workbook   'Budget Workbook
Dim y                     As Workbook   'Formatted - current workbook
Dim lastRow               As Long
Dim budgLastRow           As Long

Set y = Workbooks("DLT.xlsm")
Set z = Workbooks.Open("C:\Reports\Budget.xlsx")

'Apply function to columns to pull costing data
With y.Worksheets("DLT")
    lastRow = .Cells(.Rows.Count, 5).End(xlUp).Row

    With .Range("AI22:AJ" & lastRow)
        .Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E22,'[Budget.xlsx]DynamicReport'!H:H)"
        .Value2 = .Value2
    End With

    With .Range("AN22:AN" & lastRow)
        .Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E22,'[Budget.xlsx]DynamicReport'!E:E)"
        .Value2 = .Value2
    End With

    With .Range("AO22:AO" & lastRow)
        .Formula = "=SUMIF('[Budget.xlsx]DynamicReport'!$C:$C,$E22,'[Budget.xlsx]DynamicReport'!G:G)"
        .Value2 = .Value2
    End With

End With