loop into multiple textboxes on userform to fill formatting numbers

Mussa

Board Regular
Joined
Jul 12, 2021
Messages
241
Office Version
  1. 2019
  2. 2010
Hello

I have textboxes (textbox71,textbox78,textbox85,textbox92,textbox99,textbox106,textbox113,textbox120,textbox127,textbox134,textbox141)

and textboxes (textbox72,textbox79,textbox86,textbox93,textbox100,textbox107,textbox114,textbox121,textbox128,textbox135,textbox142)

I want filling number format when write number in any textbox then should show the number like this #,##0.00

is there any short procedure to does that by looping instead of using tradition way as known to put procedure for each textbox individually?
thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try . . .

VBA Code:
    Dim i As Long
    For i = 71 To 141 Step 7
        'first set of TextBoxes
        With Me.Controls("TextBox" & i)
            .Value = Format(.Value, "$#,##0.00")
        End With
        'second set of TExtBoxes
        With Me.Controls("TextBox" & i + 1)
            .Value = Format(.Value, "$#,##0.00")
        End With
    Next i

Hope this helps!
 
Upvote 1
Solution
Thanks , but when I call your procedure from change event for any textbox the number shows like this
write 2500 ,then shows $2.00500 based on the code , but should show 2,500.00 .how can I fix it ,please?
 
Upvote 0
I would suggest using the BeforeUpdate event for each TextBox to validate and format the entry. For example, the code for TextBox1 might be something like this...

VBA Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

    With Me.TextBox1
    
        If Len(.Value) = 0 Then Exit Sub
        
        If Not IsNumeric(.Value) Then
            Cancel = True
            .SelStart = 0
            .SelLength = Len(.Value)
            MsgBox "Please enter a number!", vbExclamation
            Exit Sub
        End If
    
        .Value = Format(.Value, "$#,##0.00")
        
    End With
    
End Sub

And the same thing for your other TextBoxes. Unfortunately, while it would normally be easier to use a class module to handle all of your TextBoxes with one event handler, the BeforeUpdate event among others are not available within a class module.

Hope this helps!
 
Upvote 0
Unfortunately, while it would normally be easier to use a class module to handle all of your TextBoxes with one event handler, the BeforeUpdate event among others are not available within a class module.
Bad news !!
this what I think to be possible .
I would suggest using the BeforeUpdate event for each TextBox to validate and format the entry
I know .
as you know sometimes when write in textbox when press enter there is no next textbox to show cursor and doesn't show format what you write the number or the user just write number without move cursor to next textbox whether by moving MOUSE or ENTER
that's why I ask about change event . may be function do that .
if you have any idea just inform me if you not I will satisfy your answering .
thanks
 
Upvote 0
Sorry, but I'm not familiar with any code that can be used in the Change event for this purpose. At best, I think it would be somewhat tricky. That's why I suggested using the BeforeUpdate event.

Cheers!
 
Upvote 0
don't worry .

just I ask you maybe you have better idea.

in reality you gives me two choices and you've solved what I want in post#2 and I prefer it, because I just put in userform module and call procedure from any textbox instead of write multiple lines as in second choice for each textbox .
two words are better than writing of multiple lines for each textbox .;)

thanks very much for your assistance .:)
 
Upvote 0
That's great, I'm glad you have what you need.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,215,426
Messages
6,124,828
Members
449,190
Latest member
rscraig11

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