我正在编程一种解析器,该解析器读取Excel表,然后创建一个
进程列表,带有一些属性,例如名称,开始时间,结束时间等。
为此,我有一个 Process 类,并且在主文件中有一个 processList (Scripting.Dictionary),在我读行时将其放置在其中...对于此分配,键是一个名为 MSID 的字符串。
现在的问题是,由于某种原因,我只能从Dictionary中访问Object并在If-ElseIf语句的一部分内更改其参数。在另一种情况下,它会引发 424个对象必需的错误,而我不知道为什么。
这是代码
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
但是在评估条件的第一行,通过调试我得到了一些不同的东西。在那!见下图
Debugging - MSID there but not able to access Dictionary entry with this as a key
您能建议如何解决此问题吗?
非常感谢您的帮助!
答案 0 :(得分:0)
所以...这有点可耻,但经过数小时的尝试解决此问题后,我发现, 我将对象添加到以MSID =“ 124”作为键的列表中。
当我尝试访问时,我当然使用了值为“ 124”的MSID。 注意区别吗?是的,最后一个空格。
棘手的部分是-VBA调试器会在字符串的末尾修剪空格, 因此实际上看不到它。同样的情况是,如果您将其打印出来-看不到...
所以最后,我花了很多时间来寻找答案,这很简单:/ 我所能做的就是嘲笑这个。