----------- Private Sub TextBox5_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean) Dim a,b as Integer a = TextBox5.Value 'テキストボックスの値を取り出す b = Format(a, "###,###") 'コンマ編集する TextBox5.Value = b 'テキストボックスへ値をセットする End Sub -----------
--------------------------------------
-------------以下Class2に記載----------
Private WithEvents KText As MSForms.TextBox
Private Index As Integer
Public Sub NewClass(ByVal t As MSForms.TextBox, ByVal i As Integer)
Set KText = t
Index = i
End Sub
Private Sub KText_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim Nt As Integer, Ht As Integer
If KeyCode = 13 Or KeyCode = 9 Then
Nt = UserForm3.Controls(t & i).Value
Ht = Format(Nt, "###,###")
UserForm3.Controls(t & i).Value = Ht
End If
End Sub
----------------------------------------
-------------以下UserForm3に記載--------
Private Ntext(5 To 50) As New Class2
Private Sub UserForm_Initialize()
Dim i As Integer, t As Integer
For i = 7 To 51 Step 4
Controls("TextBox" & i).ShowDropButtonWhen = fmShowDropButtonWhenAlways
Next i
For t = 5 To 49 Step 4
Ntext(t).NewClass Controls("Textbox" & t), t
Next t
End Sub
-----------------------------------------
ちなみにエラーはClassモジュールの
Nt = UserForm3.Controls(t & i).Value
のところで返ってきました。
今、このファイルが手元にないのでいじれないですが、多分こんな感じでしょうか?
-----------------------------------
Class2のみ修正
-----------------------------------
Private WithEvents KText As MSForms.TextBox
Private Index As Integer
Public Sub NewClass(ByVal t As MSForms.TextBox, ByVal i As Integer)
Set KText = t
Index = i
End Sub
Private Sub KText_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim Nt As Integer, Ht As Integer
If KeyCode = 13 Or KeyCode = 9 Then
Nt = UserForm3.Controls(KText & Index).Value
Ht = Format(Nt, "###,###")
UserForm3.Controls(KText & Index).Value = Ht
End If
End Sub
---------------------------------------
>>[10]
なるほど、ということはこんな感じでしょうか?
--------------------------------------
Private WithEvents KText As MSForms.TextBox
Private Index As Integer
Public Sub NewClass(ByVal t As MSForms.TextBox, ByVal i As Integer)
Set KText = t
Index = i
End Sub
Private Sub KText_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim Nt As Integer, Ht As Integer
If KeyCode = 13 Or KeyCode = 9 Then
Nt = UserForm3.Controls("TextBox" & Index).Value
Ht = Format(Nt, "###,###")
UserForm3.Controls("TextBox" & Index).Value = Ht
End If
End Sub
---------------------------------------