从用户窗体访问的全局字典对象
我在常规模块中声明了一个公共全局字典对象,如下所示:
Public dicModels As Scripting.Dictionary 'Microsoft Scripting Runtime has been added as a reference
我有以下callback:
Sub CreatePPT_OnAction(control As IRibbonControl) Call CurrentBooks Dim frmPPT_Slide As FPowerPoint Set frmPPT_Slide = New FPowerPoint frmPPT_Slide.Show Set frmPPT_Slide = Nothing End Sub
这是我的调用过程子程序:
Sub CurrentBooks() Dim wks As Excel.Worksheet Dim vObject As Variant If Not dicModels Is Nothing Then Exit Sub Set dicModels = New Dictionary For Each wks In ActiveWorkbook.Worksheets For Each vObject In wks.ListObjects If Left(vObject.Name, 3) = "TM_" Then dicModels.Add Key:=vObject.Name, Item:=Right(vObject.Name, Len(vObject.Name) - InStr(1, vObject.Name, "_")) End If Next vObject Next wks End Sub
这里是我在用户表单中初始化事件(iCounter是一个模块级别的variables声明为私人):
Private Sub UserForm_Initialize() Me.Caption = "Main Tracking Model" Me.lblModel.Caption = "Choose a model to be reflected on the PPT slide." For iCounter = 0 To dicModels.Count '<< ERROR!!!!! Me.lstModels.AddItem dicModels.Items(iCounter) Next iCounter End Sub
我想创build一个全局的字典对象可以从用户窗体类访问。 虽然我已经声明在模块级公开,我仍然得到Object variable or With block variable not set
。 我必须误解或忽略某些东西。 任何帮助表示赞赏。 谢谢!
写如下:
Public dicModels As New Scripting.Dictionary
这两个声明variables,并将其初始化为一个新的Dictionary
。
这个初始化可以和声明一起完成。 如果初始化更复杂,那么最好不要声明variablespublic,而应该有一个返回variables值的公共函数,并根据需要进行初始化:
Dim m_dicModels As Scripting.Dictionary Public Function dicModels() As Scripting.Dictionary If m_dicModels Is Nothing Then Set m_dicModels = New Scripting.Dictionary m_dicModels.CompareMode = TextCompare End If Set dicModels = m_dicModels End Function