[英]Excel VBA - Why am I getting Runtime Error
Everyone,每个人,
Can someone help me understand what is wrong with the following code?有人可以帮我理解以下代码有什么问题吗?
Sub CombDelDuplShNamCellAdd()
Dim ProdVal As New Scripting.Dictionary
ProdVal.Add "ULP", 100000
ProdVal.Add "PULP", 200000
ProdVal.Add "SPULP", 300000
ProdVal.Add "XLSD", 400000
ProdVal.Add "ALPINE", 500000
ProdVal.Add "JET", 600000
ProdVal.Add "SLOPS", 700000
Dim AdmSh, CalSh As Worksheet, ProdLinLowRan, ProdLinUppRan As Range
Set CalSh = Sheets("Calendar")
Set AdmSh = Sheets("AdminSheet")
'''' Data Prep Table
ProdLinLowRan = Range(AdmSh.Range("B10")).Offset(1).Address
ProdLinUppRan = Range(AdmSh.Range("B10")).Offset(69).Address
MsgBox ProdLinLowRan
Why am I getting this error?为什么我会收到此错误? Can't understand.无法理解。 I'm newbie btw.我是新手顺便说一句。
Cheers.干杯。
You have Dim'ed incorrectly.您的 Dim'ed 不正确。
Dim AdmSh As Worksheet
Dim CalSh As Worksheet
Dim ProdLinLowRan As Range
Dim ProdLinUppRan As Range
All these objects need a Set
.所有这些对象都需要一个Set
。
The reason for ProdLinLowRan
not failing is, that you dim it as Variant , which is very forgiving. ProdLinLowRan
没有失败的原因是,您将其调暗为Variant ,这是非常宽容的。
There are multiple issues with your code.您的代码存在多个问题。
a) Your variable declaration is flawed. a)您的变量声明有缺陷。 You will need to specify the variable type for every variable separately.您需要分别为每个变量指定变量类型。 If you write如果你写
Dim AdmSh, CalSh As Worksheet, ProdLinLowRan, ProdLinUppRan As Range
The variables AdmSh
and ProdLinLowRan
will not be of type Worksheet
resp.变量AdmSh
和ProdLinLowRan
将不属于Worksheet
类型。 Range
, they will be of type Variant
. Range
,它们将是Variant
类型。 You need to use (split into 2 lines just for readability)您需要使用(为了便于阅读,分成两行)
Dim AdmSh as Worksheet, CalSh As Worksheet
Dim ProdLinLowRan as Range, ProdLinUppRan As Range
b) AdmSh.Range("B10").Offset(69)
is already a Range. b) AdmSh.Range("B10").Offset(69)
已经是一个范围。 No need to write Range
around it.无需在其周围写Range
。
c) If you want to assign an object (and a Range is an object), you need to specify Set
. c) 如果要分配 object (并且 Range 是一个对象),则需要指定Set
。
Set ProdLinLowRan= AdmSh.Range("B10").Offset(1)
Set ProdLinUppRan = AdmSh.Range("B10").Offset(69)
What currently is going on in your code with the lines:您的代码中当前发生的情况是:
ProdLinLowRan = Range(AdmSh.Range("B10")).Offset(1).Address
ProdLinUppRan = Range(AdmSh.Range("B10")).Offset(69).Address
The right part ( Range(AdmSh.Range("B10")).Offset(1).Address
) will return the address of a range as String ( $B$11
).右侧部分( Range(AdmSh.Range("B10")).Offset(1).Address
)将返回范围的地址作为字符串( $B$11
)。 As ProdLinLowRan
is declared as Variant, VBA will write that string into ProdLinLowRan
and assume that you have a String variable.由于ProdLinLowRan
被声明为 Variant,VBA 会将该字符串写入ProdLinLowRan
并假设您有一个 String 变量。 Not what you intended, but possible.不是你想要的,但可能。
However, ProdLinUppRan
is declared as Range
, but not assigned (in VBA terms, it is Nothing
).但是, ProdLinUppRan
被声明为Range
,但未分配(在 VBA 术语中,它是Nothing
)。 Writing something into a Range writes something into a cell, eg ProdLinUppRan = 3
would write the number 3 into that cell, but only if the range variable is pointing to something.将某些内容写入 Range 会将某些内容写入单元格,例如ProdLinUppRan = 3
会将数字 3 写入该单元格,但前提是 range 变量指向某些内容。 As this is not the case, you will get the runtime error.由于情况并非如此,您将收到运行时错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.