PDA

View Full Version : Dynamic update for each userform textbox through looping



Sohan2012
12-09-2022, 12:22 PM
(CROSS POST IN THE MR EXCEL FORUM)

First of all a warm greeting to the VBA engineers, I am currently working on a Userform that has 7 text boxes in them, the following is required:

1-That all are updated when the event change is activated in one of them.

2-That have a numerical format with two decimal places.


The following code is for number formatting that is applied to each text box, but I know there is a way to do this by looping through each text box. Only my VBA level is low, many thanks in advance to whoever can help... [


Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim s$
Select Case KeyCode
Case VBA.vbKeyBack, VBA.vbKeyDelete
Caso VBA.vbKeyDown, VBA.vbKeyUp
Caso VBA.vbKeyLeft, VBA.vbKeyRight
Caso VBA.vbKeyEnd, VBA.vbKeyHome
Caso VBA.vbKeyReturn, VBA.vbKeyTab
Caso 48 a 57 '0-9
Caso 96 a 105
Caso 188, 110, 190 : ', .
KeyCode = 188
If InStr(1, TextBox1.Text, ",") > 0 Then KeyCode = 0
Case 109, 189: '-
KeyCode = 0
If InStr(1, TextBox1.Text, "-") = 0 Then
TextBox1. Texto = "-" & TextBox1.Text
Else
TextBox1.Text = VBA.Replace(TextBox1.Text, "-", ""
End Select

georgiboy
12-16-2022, 05:44 AM
Not sure if you got an answer on MrExcel as there was no link provided.

below is a method of looping through controls:


Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 5
Controls("TextBox" & i).Value = "Hello World"
Next i
End Sub