Excel: VBA: Format Group of textboxes on forms

BIGTONE559

Active Member
Joined
Apr 20, 2011
Messages
336
Need helping with formatting all textboxes on a form.

i have the following code:
Code:
If TextBox1.Text = "" Then
TextBox1.Text = "0:00"
Else
TextBox1.Text = Format(Format(TextBox1.Text, "##:##"), "Medium Time")
End If

I want to apply this code to a group of textboxes exit event without having to go to each textbox event and type the code. Is there a way to do this?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You could put that code in its own subroutine which is called by each text box Exit event, like this:
Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Format_TextBox TextBox1
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Format_TextBox TextBox2
End Sub

Private Sub Format_TextBox(theTextBox As MSForms.Control)
    If theTextBox.Text = "" Then
        theTextBox.Text = "0:00"
    Else
        theTextBox.Text = Format(Format(theTextBox.Text, "##:##"), "Medium Time")
    End If
End Sub
Or you could write a custom text box class event handler to handle events from your text boxes (i.e. the Exit event). http://j-walk.com/ss/excel/tips/tip44.htm shows the basic technique.
 
Upvote 0
You could put that code in its own subroutine which is called by each text box Exit event, like this:
Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Format_TextBox TextBox1
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Format_TextBox TextBox2
End Sub

Private Sub Format_TextBox(theTextBox As MSForms.Control)
    If theTextBox.Text = "" Then
        theTextBox.Text = "0:00"
    Else
        theTextBox.Text = Format(Format(theTextBox.Text, "##:##"), "Medium Time")
    End If
End Sub
Or you could write a custom text box class event handler to handle events from your text boxes (i.e. the Exit event). http://j-walk.com/ss/excel/tips/tip44.htm shows the basic technique.


Perfect Timing John. I appreciate it. I think i'll just define the function as you suggested.

thanks i'll try and post results
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,734
Members
452,939
Latest member
WCrawford

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