Consulting

Results 1 to 2 of 2

Thread: Dynamic update for each userform textbox through looping

  1. #1

    Dynamic update for each userform textbox through looping

    (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
    Last edited by Aussiebear; 12-09-2022 at 01:08 PM. Reason: Tidy up code presentation

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    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
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •