VBA函数在结束函数上中断:“对象不支持此属性或方法”

时间:2019-11-14 01:16:04

标签: excel vba

我正试图返回看起来像这样的自定义类XportTemplateRow

Option Explicit

Public Token As ShapeToken
Public Index As Integer
Public Value As Variant

通过我的功能验证电子表格中的输入。但是,我的函数无法成功执行,并引发错误“对象不支持此属性或方法”。当它到达End Function行时。

监视窗口(如下所示)显示已按预期分配值。行数据与此类似,其中省略号是许多行,其中包含此函数未使用的数据。 :

index
 |
 |  token
 |    |
 |    |          value
 V    V            V
[1, NONE, ..., 0             , ...]
[2, HEAD, ..., "someItemName", ...]
[3, NONE, ..., 1234          , ...]
 ...

我不确定这是怎么回事。如果从倒数第二行删除Set,则会收到错误“对象变量或未设置变量”。我不知道如何继续调试。

引起问题的函数如下:

Public Function getXportTemplateRow(rowNumber As Long, template As XportTemplate, _
                                    MasterFileSpecSheet As Worksheet) _
                                    As XportTemplateRow
    ' -----------------------------------------------------------
    ' Return an XportTemplateRow by parsing the XportTemplate at given row.
    ' -----------------------------------------------------------
    Dim outputRow As XportTemplateRow
    Set outputRow = New XportTemplateRow
    Dim tokenDict
    Set tokenDict = getTokenDict()
    ' Build tokenDict from settings file

    outputRow.Index = template.IndexRng.Cells(rowNumber, 1)
    outputRow.Token = tokenDict(template.TokenRng.Cells(rowNumber, 1))

    ' May need to validate here
    outputRow.Value = getXportValue(template.ConstRng.Cells(rowNumber, 1), _
                                    template.LookupRng.Cells(rowNumber, 1), _
                                    MasterFileSpecSheet)

    Set getXportTemplateRow = outputRow
End Function ' <--- Debugger breaks on this line
  

我希望此函数返回包含三个值的XportTemplateRow类:(索引,令牌,值)

enter image description here


编辑1:

我这样调用函数:


Dim template As XportTemplate
Set template = New XportTemplate
' ...
' Code to populate template
' ...

Dim i As Long
For i = LBound(skuWorksheet) To Ubound(skuWorksheet)
    Dim xportRow As XportTemplateRow
    Set xportRow = New XportTemplateRow
    xportRow = getXportTemplateRow(i + 1, template, skuWorksheet) ' (Range index from 1)
    ' ...
    ' do things with xportRow
    ' ...
Next i

编辑2:最小可复制示例

Public Sub TestXport()
    Dim specSheet As Worksheet
    Set specSheet = Worksheets("PRODUCTS")

    Dim xportTemplateSheet As Worksheet
    Set xportTemplateSheet = Worksheets("PRODUCTS_Template")

    Dim template As XportTemplate
    Set template = New XportTemplate

    Set template.IndexRng = xportTemplateSheet.Range("A9:A16")
    Set template.ConstRng = xportTemplateSheet.Range("D9:D16")
    Set template.TokenRng = xportTemplateSheet.Range("C9:C16")
    Set template.LookupRng = xportTemplateSheet.Range("E9:E16")

    Dim xportRow As XportTemplateRow
    Set xportRow = New XportTemplateRow
    xportRow = getXportTemplateRow(0 + 1, template, specSheet) ' <--- Breaks within
    Debug.Print xportRow.Index
    Debug.Print xportRow.Token
    Debug.Print xportRow.Value
End Sub

解决方案

    Dim xportRow As XportTemplateRow
    Set xportRow = New XportTemplateRow
    xportRow = getXportTemplateRow(i + 1, template, skuWorksheet) ' (Range index from 1)

成为

    Dim xportRow As XportTemplateRow
    Set xportRow = getXportTemplateRow(i + 1, template, skuWorksheet) ' (Range index from 1)

0 个答案:

没有答案