在 VBA 的不同子目录中访问字典
Access Dictionary in Different Subs in VBA
我想使用字典在 VBA (Excel) 中存储一些状态,但在不同的子目录中访问它时遇到问题。
最后,我想要一个包含 ID 作为主键的嵌套字典,并且对于每个 ID,我想要多个具有匹配时间戳的状态消息:
Example: {ID_1: {status_1: 09.10.19 06:47, status_2: 09.10.19 07:00,...}, ID_2:{status_6: 09.10.19 06:30, status_1: 09.10.19 06:20}}
到目前为止,我只是想获取一个仅包含 ID 和状态的简单字典:
Option Explicit Public dict As Scripting.Dictionary Public id As Integer
Public Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Fin
Dim current_status As String
current_status = Tabelle4.Range("B11").Value
id = Tabelle3.Cells(7, 6).Value
Application.EnableEvents = False
If Not Intersect(Target, Range("C12:G12")) Is Nothing Then
If dict Is Nothing Then
Set dict = New Scripting.Dictionary
dict.Add (id), current_status
'Checks if inner dict exists
ElseIf Not dict(id).Exists Then
dict.Add (id), current_status
End If
End If
Fin:
Application.EnableEvents = True
End Sub
请注意,我手动更改了 ID,通过将 C12 中的其中一个值更改为 G12,将在 B11 中生成 current_status(这就是我使用 Change 事件的原因)。
最后我想记录所有发生的状态更新并跟踪它们发生的时间。
我的问题是这对 1 个条目有效。我可以在 "If dict is Nothing Then" 部分创建一个条目,甚至可以在活动结束后访问它。 (例如,在再次触发事件之前在开头插入 MsgBox dict(ID)。)
我还想使用 Excel 上的按钮打印结果。
但是,当我打电话时:
Public Sub print_dict()
MsgBox dict(id)
End Sub
我收到一条错误消息,指出没有定义字典。 "MsgBox id" 产生一个空的 MsgBox。
也许有人可以帮助我。我是 VBA 的新手,并且还在为之苦苦挣扎 :D
干杯!
dict(id).Exists
应该是
dict.Exists(id)
使用错误处理程序时,值得确保您不会只是跳过代码语法问题。
我猜 MsgBox dict(id)
失败是因为该子模块在常规模块中,而不在您的 sheet 模块中。
MsgBox sheetCodename.id
不过应该可以。
MsgBox id
给你一个空的消息框,因为你没有在常规代码模块中使用 Option Explicit
:变量 id
不存在于可以访问的范围内通过您的 Sub ,但没有 Option Explicit
您的代码会自动为您创建一个(空白) id
变量。
我想使用字典在 VBA (Excel) 中存储一些状态,但在不同的子目录中访问它时遇到问题。
最后,我想要一个包含 ID 作为主键的嵌套字典,并且对于每个 ID,我想要多个具有匹配时间戳的状态消息:
Example: {ID_1: {status_1: 09.10.19 06:47, status_2: 09.10.19 07:00,...}, ID_2:{status_6: 09.10.19 06:30, status_1: 09.10.19 06:20}}
到目前为止,我只是想获取一个仅包含 ID 和状态的简单字典:
Option Explicit Public dict As Scripting.Dictionary Public id As Integer
Public Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Fin
Dim current_status As String
current_status = Tabelle4.Range("B11").Value
id = Tabelle3.Cells(7, 6).Value
Application.EnableEvents = False
If Not Intersect(Target, Range("C12:G12")) Is Nothing Then
If dict Is Nothing Then
Set dict = New Scripting.Dictionary
dict.Add (id), current_status
'Checks if inner dict exists
ElseIf Not dict(id).Exists Then
dict.Add (id), current_status
End If
End If
Fin:
Application.EnableEvents = True
End Sub
请注意,我手动更改了 ID,通过将 C12 中的其中一个值更改为 G12,将在 B11 中生成 current_status(这就是我使用 Change 事件的原因)。 最后我想记录所有发生的状态更新并跟踪它们发生的时间。
我的问题是这对 1 个条目有效。我可以在 "If dict is Nothing Then" 部分创建一个条目,甚至可以在活动结束后访问它。 (例如,在再次触发事件之前在开头插入 MsgBox dict(ID)。) 我还想使用 Excel 上的按钮打印结果。 但是,当我打电话时:
Public Sub print_dict()
MsgBox dict(id)
End Sub
我收到一条错误消息,指出没有定义字典。 "MsgBox id" 产生一个空的 MsgBox。
也许有人可以帮助我。我是 VBA 的新手,并且还在为之苦苦挣扎 :D
干杯!
dict(id).Exists
应该是
dict.Exists(id)
使用错误处理程序时,值得确保您不会只是跳过代码语法问题。
我猜 MsgBox dict(id)
失败是因为该子模块在常规模块中,而不在您的 sheet 模块中。
MsgBox sheetCodename.id
不过应该可以。
MsgBox id
给你一个空的消息框,因为你没有在常规代码模块中使用 Option Explicit
:变量 id
不存在于可以访问的范围内通过您的 Sub ,但没有 Option Explicit
您的代码会自动为您创建一个(空白) id
变量。