简体   繁体   English

Excel VBA - 为什么我收到运行时错误

[英]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.变量AdmShProdLinLowRan属于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.

相关问题 为什么我在Excel VBA上遇到运行时错误 - Why Am I getting a run time error on Excel VBA 为什么我得到Object Required错误(VBA,Excel) - Why am I getting Object Required error (VBA, Excel) 我正在VBA Excel中编写编码器。 我不断收到运行时错误13。它说不匹配 - I am writting a encoder in VBA excel. I keep getting a runtime error 13. It says there is a mismatch 为什么会出现此运行时错误? - Why am I getting this runtime error? VBA运行时错误:为什么在使用mailenvelope通过Outlook发送邮件时出现错误 - VBA runtime error : Why am i getting an error while sending mail through outlook using mailenvelope 如何在vba中存储范围的行和列号,为什么会出现438运行时错误? - How can I store the row and column numbers of a range in vba, and why am I getting a 438 runtime error? 我在 VBA 中使用 Excel,它间歇性地引发运行时错误 1004 - I am using Excel with VBA, and it intermittently raises a runtime error 1004 为什么我在VBA中查找错误? - Why am I getting an error with Find in VBA? 为什么在Excel中使用VBA会收到“运行时错误'424':需要对象”? - Why am I getting “run-time error '424': object required” using VBA in Excel? 为什么我的Excel VBA代码中出现“语句结束”编译错误? - Why am I getting an “End of Statement” compile error in my Excel VBA code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM