对象定义的错误,将命名范围分配给数组

时间:2013-01-18 18:10:47

标签: excel-vba vba excel

我正在尝试将命名范围的值分配给变量数组。我几乎在赋值代码的级别上得到了一个应用程序或对象定义的错误     Material = ThisWorkbook.Names(“RMInStoreName”)。RefersToRange 我从here得到了这个例子,并且在成功之前使用过它。

我仍然想弄清楚为什么这个错误出现了,我似乎已经用完了想法,任何人都可以帮我指出正确的方向,真的会救我很多 代码如下 这是代码

 Sub MonitorStore()
Dim ThreshHold As Variant, InStore As Variant, StatusReport As Variant
Dim Material As Variant    'is the name of the material
Status As Variant
'status is a variable which holds data on wether the user  has seen msg and 
'wants to     supress msg
'the ThreshHold is the minimum allowed in store below which messages are firerd
'InStore is the volume of materials currently in store
'and be told of another error after solving error one, report all at once


 Material = ThisWorkbook.Names("RMInStoreName").RefersToRange
ThreshHold = ThisWorkbook.Names("RMThreshHold").RefersToRange
InStore = ThisWorkbook.Names("RMInStore").RefersToRange
Status = ThisWorkbook.Names("RMStatus").RefersToRange

'other code.............
'dont expect error from unexecuted code
End Sub

感谢您的帮助 斯蒂芬

2 个答案:

答案 0 :(得分:0)

最可能的原因是您没有在工作簿中定义所有命名范围。

您可以使用“公式”选项卡验证命名范围。

公式 - >名称经理

enter image description here

答案 1 :(得分:0)

RefersToRange将返回一个范围对象。它是你之后的价值:

Material = ThisWorkbook.Names("RMInStoreName").RefersToRange.Value

或者您可以使用:

Material = Evaluate("RMInStoreName")

或:

Material = Evaluate(ThisWorkbook.Names("RMInStoreName").RefersTo).Value