我是編碼類模塊的新手,但在普通模塊中相當不錯......現在我正在嘗試回圈我制作的集合但沒有成功。我可以展開并查看 VBE 中的“元素”,但得到錯誤:“物件不支持此屬性或方法”。
在一個標準模塊中,我有
Public collItems As Collection
該集合在初始化時填充在用戶表單模塊中
collItems.add cItems
cItems是一個由 組成的物件New clsItems,它是類模塊。
類模塊由許多用戶表單控制元件組成,如下所示:
Private WithEvents frm As msforms.Frame
Private WithEvents lbl As msforms.Label
Private WithEvents cmd As msforms.CommandButton
Private WithEvents txt1 As msforms.TextBox
Private WithEvents txt2 As msforms.TextBox
不知道是否Private是這樣的方式去......
當用戶表單完成加載時,會出現動態數量的幀,每個幀內都有所有這些文本框。它看起來像一個由 MS Project 中的資料制成的電子表格。命令按鈕的作業是更改許多文本框的屬性/屬性。所以現在我想回圈collItems收集并撰寫我的代碼。但后來我得到一個錯誤......
我沒有get 或let 在我的班上。只是一個單一的set財產。在類中添加 10 個獨特的文本框可能看起來很愚蠢,但這就是我到目前為止的作業方式......所有表單物件都被賦予了在創建程序中參考行和列的名稱,并幫助我識別它們。
失敗的代碼如下所示:
Sub changeBox(ByRef name As String)
For Each item in collItems.item(CLng(Replace(name, "cmd", "")))
'blabla
Next item
End Sub
該測驗有效并顯示了我想要回圈但不能的所有元素:
Set test = collItems.item(3) 'Meaning row 3 in userform
Any help on what I'm doing wrong or missing, in order to loop my specific textboxes and change their attributes would be much appreciated!
uj5u.com熱心網友回復:
所以現在我想回圈 collItems 集合并撰寫我的代碼。但后來我得到一個錯誤......
該集合包含一系列型別的物件cItems。因此,要回圈集合項,請宣告此型別的物件用作迭代器。
請注意,類成員必須公開才能在cItems類外訪問。不確定如何實體化它們,但它們可以通過公共只讀屬性公開。
Public Property Get Form() As msforms.Frame
Set Form = frm
End property
回圈:
Dim objCurrent As cItems, frm As msforms.Frame
For Each objCurrent in collItems
'here you can use the objCurrent to access the current item.
Set frm = objCurrent.Form
'...
Next objCurrent
為了能夠按名稱獲取控制元件,請在cItems類中創建一個字典來存盤控制元件并使用鍵來檢索物件。
請參見下面的示例:
'dictionary
Private m_boxes As Object
Public Sub AddTextBox(ByVal Key As String, ByVal obj As msforms.TextBox)
m_boxes.Add Key, obj
End Sub
Public Function GetTextBox(ByVal Key As String) As msforms.TextBox
Set GetTextBox = m_boxes(Key)
End Function
Private Sub Class_Initialize()
Set m_boxes = CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set m_boxes = Nothing
End Sub
呼叫它:
Dim txt As msforms.TextBox
Set txt = objCurrent.GetTextBox("txt1")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/440559.html
上一篇:使用“小計”格式化單元格
