简体   繁体   中英

excel macro application-defined or object-defined error

I am using the below code and get the error. The code is used to take a group of sigle items and group them and their individula corresponding values together.

application-defined or object-defined error When the code reaches the line: `.Range("B15:B" & LastRow).Formula = "=SUMIF(" & myrng1.Address & ",RC[-1]," & myrng2.Address & ")"

The error is with the middle section of the code RC[-1] If someone could please tell me what i shuld use in place of this, it would be of great assistance `

The code is

Sub InventorySummerise()

Dim LastRow As Long
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
Set myrng1 = Range("B2:B" & LastRow)
Set myrng2 = Range("D2:D" & LastRow) 
Cells(LastRow + 5, 2).Select
With ThisWorkbook.Sheets("Sheet1")
    .Range("B15:B" & LastRow).Formula = "=SUMIF(" & myrng1.Address & ",RC[-1]," & myrng2.Address & ")"
End With

End Sub

Any help with what is wrong would be amazing, im at wits end with it

Thank you!

two ways to get this done:

One:

'~~> change RC[-1] reference to A1 notation
'~~> so if you are putting formula in B15, RC[-1] is A15
With ThisWorkbook.Sheets("Sheet1")
    .Range("B15:B" & LastRow).Formula = "=SUMIF(" & myrng1.Address & ",A15," & myrng2.Address & ")"
End With

Two:

'~~> As what Patrick said, use FormulaR1C1
'~~> Then change R1C1 address reference style for your ranges
With ThisWorkbook.Sheets("Sheet1")
    .Range("B15:B" & LastRow).FormulaR1C1 = "=SUMIF(" & myrng1.Address(ReferenceStyle:=xlR1C1) & ",RC[-1]," & myrng2.Address(ReferenceStyle:=xlR1C1) & ")"
End With

hope this helps.

You are trying to use R1C1 formulas to normal Formula.

Should use .Range("B15:B" & LastRow).FormulaR1C1 = "..."

Try this:

Sub InventorySummerise()
    On Error Resume Next
    Dim LastRow As Long, myrng1 As Range, myrng2 As Range
    LastRow = Cells(Rows.Count, "B").End(xlUp).Row
    Set myrng1 = Range("B3:" & Cells(LastRow, 2).Address(rowabsolute:=False, columnabsolute:=False))
    Set myrng2 = Range("D3:" & Cells(LastRow, 4).Address(rowabsolute:=False, columnabsolute:=False))

    With ThisWorkbook
        .Names("Range1").Delete
        .Names("Range2").Delete
        .Names.Add "Range1", myrng1
        .Names.Add "Range2", myrng2
        .Sheets("Sheet1").Range("B15:B" & LastRow + 5).Formula = "=SUMIF(Range1,RC[-1],Range2)"
    End With
End Sub

Hopefully It has been already resolved for you. If that's not the case, error comes from the .range not recognize. If activating workbook does not bother you, you could try the following:

Sub InventorySummerise()
Dim wtb As Workbook
Dim LastRow As Long
Set wtb = ThisWorkbook
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
Set myrng1 = Range("B3:" & Cells(LastRow, 2).Address(rowabsolute:=False, columnabsolute:=False))
Set myrng2 = Range("D3:" & Cells(LastRow, 4).Address(rowabsolute:=False, columnabsolute:=False))
Cells(LastRow + 5, 2).Select
wtb.Activate
With Sheets("Sheet1")
.Range("B15:B" & LastRow).Formula = "=SUMIF(" & myrng1.Address & ",1," & myrng2.Address & ")"
End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM