Dynamic update for each userform text box via loops (cross post in VBA EXPRESS)

kapela2017

New Member
Joined
Oct 16, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
First of all best regards VBA engineers, I am currently working on a UserForm that has 7 text boxes, the following is required:

1-That they all update when the event changes active in one of them.

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


VBA Code:
The following code is for the number format 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 anyone who 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

       Case VBA.vbKeyDown, VBA.vbKeyUp

       Case VBA.vbKeyLeft, VBA.vbKeyRight

       Case VBA.vbKeyEnd, VBA.vbKeyHome

       Case VBA.vbKeyReturn, VBA.vbKeyTab

       Case 48 To 57       '0-9

       Case 96 To 105

       Case 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.Text = "-" & TextBox1.Text

            Else

                TextBox1.Text = VBA.Replace(TextBox1.Text, "-", "")

            End If

       Case Else: KeyCode = 0

End Select


End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Cross-posting is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.
(Note that you need to post a link to the Cross-Posted article on the other forums, so please do that!)
 
Upvote 0
First of all best regards VBA engineers, I am currently working on a UserForm that has 7 text boxes, the following is required:

1-That they all update when the event changes active in one of them.

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


VBA Code:
The following code is for the number format 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 anyone who 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

       Case VBA.vbKeyDown, VBA.vbKeyUp

       Case VBA.vbKeyLeft, VBA.vbKeyRight

       Case VBA.vbKeyEnd, VBA.vbKeyHome

       Case VBA.vbKeyReturn, VBA.vbKeyTab

       Case 48 To 57       '0-9

       Case 96 To 105

       Case 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.Text = "-" & TextBox1.Text

            Else

                TextBox1.Text = VBA.Replace(TextBox1.Text, "-", "")

            End If

       Case Else: KeyCode = 0

End Select


End Sub
Here is the cross post thread
 
Upvote 0
Cross-posting is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.
(Note that you need to post a link to the Cross-Posted article on the other forums, so please do that!)
Greetings boss, check my thread and leave the link, let me know if it looks good to you..
 
Upvote 0

Forum statistics

Threads
1,214,605
Messages
6,120,473
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top