VBAオブジェクトを自動生成しイベントを付与する。
2019.01.12
VBAオブジェクトを自動生成しイベントを付与する。
サンプルコードです。
ダウンロードはこちら
https://zip358.com/tool/sample.zip
Dim chg_class(0 To 5) As chg
Public Sub objset()
Dim obj_ctl As Control
Dim i As Integer
For i = LBound(chg_class) To UBound(chg_class)
Set obj_ctl = UserForm1.Controls.Add("Forms.TextBox.1", "Box" & i)
obj_ctl.Top = 10 + 20 * i
obj_ctl.Width = 200
obj_ctl.Height = 20
obj_ctl.Text = "ここを変更してみて、またはダブルクリック(" & i & "番)"
Set chg_class(i) = New chg
chg_class(i).set_evn obj_ctl, i
Set obj_ctl = Nothing
Next i
End Sub
Private WithEvents TextB As MSForms.TextBox
Private index_no As Integer
Public Sub set_evn(hoge_obj As MSForms.TextBox, hoge As Integer)
Set TextB = hoge_obj
index_no = hoge
End Sub
Private Sub TextB_Change()
MsgBox TextB.Text 'UserForm1("Box" & index_no)
End Sub
Private Sub TextB_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
MsgBox TextB.Text 'UserForm1("Box" & index_no)
End Sub
Private Sub TextBox1_Change()
MsgBox TextBox1
End Sub
Private Sub CommandButton1_Click()
End Sub
Private Sub UserForm_Click()
End Sub
タグ
0, 1, 10, 20, 200, 358, 5, Add, amp, as, Box, chg, class, com, Control, Controls, ctl, Dim, For, Forms, Height, Integer, LBound, obj, objset, public, sample, set, Sub, Text, TextBox, To, tool, TOP, UBound, UserForm, VBA, Width, zip, イベント, オブジェクト, コード, ここ, こちら, サンプル, ダウンロード, 付与, 変更, 自動生成,