I'm trying to return my custom class XportTemplateRow
which looks like this:
Option Explicit
Public Token As ShapeToken
Public Index As Integer
Public Value As Variant
From my function that validates input from a spreadsheet. However, my function is unable to execute successfully, raising the error "Object doesn't support this property or method." when it gets to the End Function
line.
The watch window (attached below) shows that the values are being assigned as expected. The row data looks similar to this, where ellipses are many rows with data that isn't used in this function. :
index
|
| token
| |
| | value
V V V
[1, NONE, ..., 0 , ...]
[2, HEAD, ..., "someItemName", ...]
[3, NONE, ..., 1234 , ...]
...
I'm not sure what's going wrong. If I remove Set
from the second-to-last line I get the error "Object variable or with variable not set." I don't know how to continue debugging this.
The function causing problems looks like this:
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
I want this function to return the
XportTemplateRow
class containing the 3 values: (Index, Token, Value)
Edit 1:
I call the function like this:
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
Edit 2: Minimum Reproducible Example
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
Solution
Dim xportRow As XportTemplateRow
Set xportRow = New XportTemplateRow
xportRow = getXportTemplateRow(i + 1, template, skuWorksheet) ' (Range index from 1)
becomes
Dim xportRow As XportTemplateRow
Set xportRow = getXportTemplateRow(i + 1, template, skuWorksheet) ' (Range index from 1)