简体   繁体   English

为什么在Excel中使用VBA会收到“运行时错误'424':需要对象”?

[英]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. 我在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! 任何帮助将不胜感激!

On the last line, the one highlighted, copyrange does not exist. 在最后一行上,突出显示的那个,copyrange不存在。 copyrng does, small difference. 复制确实,差异很小。

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. 对于变体,请使用Value2或复制一个范围。

From Help for Dim 从帮助昏暗

type Optional . 输入 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. 可以是Byte,Boolean,Integer,Long,Currency,Single,Double,Decimal(当前不支持),Date,String(对于可变长度的字符串),String * length(对于固定长度的字符串),Object,Variant,a用户定义的类型或对象类型。 Use a separate As type clause for each variable you declare . 对声明的每个变量使用单独的As类型子句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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