【发布时间】:2018-11-07 23:39:51
【问题描述】:
我正在编写一种解析器,它读取一个 Excel 表,然后创建一个
进程列表,包含一些属性,例如Name、StartTime、EndTime等。
为此,我有一个类 Process 并且在主文件中,我有一个 processList (Scripting.Dictionary),当我阅读这些行时,我将进程放置在其中...对于此分配,key 是一个名为 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
但是在评估条件的第一行,我通过调试得到了不同的结果。在那里!见下图
Debugging - MSID there but not able to access Dictionary entry with this as a key
你能建议如何解决这个问题吗?
非常感谢您的帮助!
【问题讨论】:
-
最好的猜测是您从未添加过该 MSID,因为它位于(未指定)
If子句的另一部分。 -
将访问权限封装在
If processList.Exists(MSID) Then中。您假设您从processList(MSID)获得结果。 -
我更新了问题。此 if 语句的计算结果为 false(在字典中未找到)。但是,当我调试时,我看到它具有完全相同的 MSID。这对我来说绝对没有意义......即使我使用
If processList.Exists("124") Then访问它也不起作用
标签: excel vba dictionary object scripting.dictionary