VBA中出现“需要对象”错误,但是我正在使用Set来分配对象

时间:2018-08-28 21:43:18

标签: vba excel-vba

我有这个功能:

template <typename T>
struct is_bananas<T, value_void_t<T::config::num_items>>
   : std::true_type {};

Public Function nextBlock(featureBlockObj As FeatureBlock, endFeatureNumber As Variant) As FeatureBlock Dim downstreamFeatureNumber As Variant 'get the feature number that this endpoint connects to downstreamFeatureNumber = featureBlockObj.connectedFeatureNumber(endFeatureNumber) 'if it connects to a feature number, look that feature number up in blockDict If isNumericNonBlank(downstreamFeatureNumber) Then Set nextBlock = blockDict(downstreamFeatureNumber) 'if it does not connect, this is the last block in the sequence Else Set nextBlock = Nothing End If End Function 是字典; blockDict是用户定义的类。

此行:

FeatureBlock

抛出错误:

Set nextBlock = blockDict(downstreamFeatureNumber)

我已验证以下内容:

  • Object required. 作为downstreamFeatureNumber中的密钥存在
  • blockDictblockDict(downstreamFeatureNumber)类型的对象

那我为什么不能使用FeatureBlockblockDict()引用分配给nextBlock

1 个答案:

答案 0 :(得分:1)

很明显,blockDict(downstreamFeatureNumber)没什么,这是您的问题。在尝试分配给nextBlock之前,您需要先对其进行限定(如果不应该发生,那么这将是一个完全不同的问题-不是VBA问题)。

If isNumericNonBlank(downstreamFeatureNumber) _
    AND (Not (blockDict(downstreamFeatureNumber) Is Nothing)) Then