0


我正在编写一种解析器,它读取一个 Excel 表,然后创建一个 进程列表,其中包含一些属性,如Name、StartTime、EndTime等。为此,我有一个Process类,在主文件中,我有一个processList(脚本。字典),当我阅读这些行时,我把进程放在了那里......对于这个作业,关键是一个名为MSID的字符串。

现在的问题是,出于某种原因,我只能从字典中访问对象并在我的 If-ElseIf 语句的一部分内更改其参数。在另一种情况下,它会引发424-object required 错误,我不知道为什么。

这是代码

Sub ParseMessages()
' ITERATOR VARIABLES
Dim wb As Workbook, ws As Worksheet
Dim rowIter As Long, row As Variant
Dim A As Variant, B As Variant, C As Variant, D As Variant, E As Variant, F As Variant  ' A,B,C,D,E,F variables for the cells of each row

' PROCESS PARAMETERS
Dim MSID As Variant
Dim StartTime As Variant
Dim EndTime As Variant

' OBJECTS
Dim process As process
Dim processList As Scripting.Dictionary     ' DICTIONARY where the error happens
Set processList = New Scripting.Dictionary

Worksheets(1).Activate

'####### MAIN LOOP ######################################################
For rowIter = 1 To 11
    row = Rows(rowIter)
    A = row(1, 1)
    B = row(1, 2)
    C = row(1, 3)
    D = row(1, 4)
    E = row(1, 5)
    F = row(1, 6)
    Dim startIndex As Long, endIndex As Long, count As Long

    ' ------ PROCESSSTART -> MSID, processName, startTime
    If (.....) Then
        Debug.Print (vbNewLine & "Process start")
        If (...) Then   ' --MSID
            startIndex = InStr(F, "Nr") + 3             '3 to skip "Nr "
            endIndex = InStr(startIndex, F, "(")
            count = endIndex - startIndex
            MSID = Mid(F, startIndex, count)
            StartTime = B
            Debug.Print (StartTime & " -> " & MSID)

            ' **** MAKE new Process object, add to collection
            Set process = New process
            process.StartTime = StartTime
            process.MSID = MSID
            processList.Add MSID, process    ' Add to the dictionary, KEY, VALUE

        ElseIf (...) Then      ' --ProcessName
            startIndex = InStr(F, "=") + 2
            endIndex = InStr(F, "<") - 1
            count = endIndex - startIndex
            processName = Mid(F, startIndex, count)
            Debug.Print (processName)
            ' **** Add Name to the last element of the dictionary
            processList(processList.Keys(processList.count - 1)).Name = processName 'get last Process Object
            processList(MSID).Name = "Just Testing" ' !!!! here it works
        Else
        End If

    ' ------ END OF PROCESS ->
    ElseIf (......) Then
        startIndex = InStr(D, "MSID") + 5
        endIndex = InStr(startIndex, D, "]")
        count = endIndex - startIndex
        MSID = Mid(D, startIndex, count)

        EndTime = B
        Debug.Print (EndTime & " End of process " & MSID)

        ' **** Add End time for the process from the collection, specified by MSID
        Debug.Print ("Test of " & processList(MSID).Name)  ' !!!!! Doesn't work
        processList(MSID).Name = "Just Prooooooving"       ' !!!!! Here doesn't work
        processList(MSID).EndTime = EndTime                ' !!!!! Does not work
    End If
Next
End Sub

因此,要指定问题-为什么会这样:

processList(MSID).Name = "Just Testing" ' !!!! here it works

这不会:

processList(MSID).Name = "Just Prooooooving"       ' !!!!! Here doesn't work

如果我首先证明具有 MSID 键的对象是否存在于字典中,则找不到。

If processList.Exists(MSID) Then
            Debug.Print ("Process exists, altering it...")
            processList(MSID).Name = "Just Prooooooving"   ' !!!!! Here doesn't work
            processList(MSID).EndTime = EndTime
End If

但是在评估条件的第一行,我通过调试得到了一些不同的东西。在那里!见下图

调试 - 那里有 MSID,但无法使用这个作为键访问字典条目

你能建议如何解决这个问题吗?

非常感谢您的帮助!

4

1 回答 1

0

所以......这有点可耻,但在尝试解决这个问题几个小时后我发现,我将对象添加到列表中,并以 MSID="124" 作为键。

当我尝试访问时,我当然使用了值为“124”的 MSID。注意到区别了吗?是的,最后那个空间。

棘手的部分是 - VBA 调试器修剪字符串末尾的空格,因此实际上不可能看到它。同样的情况是,如果您将其打印出来-不可能看到...

所以最后,我花了很多时间寻找答案,这很简单:/我所能做的就是对此一笑置之。

于 2018-11-07T15:39:51.347 回答