James Beaulieu
James Beaulieu

Reputation: 55

Why am I getting "run-time error '424': object required" using VBA in Excel?

I'm having an issue with some code I'm writing in VBA. I'm getting this error, but I don't think I'm using it wrong, since I wrote something very similar in another sub.

Sub quicktest()
Dim testrng As Range
Set testrng = Sheet5.Range("J81")
AddNewRecordsColumn testrng
End Sub

Sub AddNewRecordsColumn(bottomcell As Range)
Dim copyrng, pasterng As Range
Set copyrng = Sheet5.Range(Sheet5.Range("E3"), bottomcell)
Set pasterng = Sheet5.Range(Sheet5.Range("D3"), bottomcell)
pasterng.Value = copyrange.Value 'this is the line highlighted by debugger

End Sub

Any help would be greatly appreciated!

Upvotes: 0

Views: 900

Answers (2)

Noodles
Noodles

Reputation: 2011

Dim copyrng, pasterng As Range

Because a data type hasn't been specified for copyrng it is a variant. For Variants use Value2 or make copyrng a range.

From Help for Dim

type Optional. Data type of the variable; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. Use a separate As type clause for each variable you declare.

Upvotes: 1

Juru
Juru

Reputation: 1629

On the last line, the one highlighted, copyrange does not exist. copyrng does, small difference.

Upvotes: 3

Related Questions