Automatically insert : for hhmm textbox?

renraw9002

New Member
Joined
Jun 20, 2024
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I have multiple userforms for a spreadsheet and some textboxes require time entry. We always write time using hhmm and using 4 digits. I'm not really worried about people accidentally typing in letters or using 3 numbers or anything like that. I'd just like it so that when people type 0123, 1234, etc it will automatically insert a : between the second and third numbers.

If there isn't a simple way to just automatically insert the colon between the 2nd and 3rd numbers is there a simple code I can put in to check the textbox for a colon and if it's missing, alert the user to insert one and try hitting save again?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
@renraw9002, welcome to MrExcel
Maybe something like this:
VBA Code:
Private Sub TextBox1_Change()
With TextBox1
If .Text Like "####" Then 'if the text is 4 digit
   .Text = Left(.Text, 2) & ":" & Right(.Text, 2)
End If
End With
End Sub
 
Upvote 0
Solution
@renraw9002, welcome to MrExcel
Maybe something like this:
VBA Code:
Private Sub TextBox1_Change()
With TextBox1
If .Text Like "####" Then 'if the text is 4 digit
   .Text = Left(.Text, 2) & ":" & Right(.Text, 2)
End If
End With
End Sub
I could kiss you. I've been searching for weeks for this. Seen codes very similar, but it was never on textbox change and it never worked. It even works for me on both the search function and when saving info which is even better than I had hoped. Thank you so much.
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0
Additionally, if you want, you can add a validation on Exit event, like this:
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Text <> "" Then
    If Not TextBox1.Text Like "##:##" Then
        MsgBox "Wrong input"
        Cancel = True
    End If
End If
End Sub
It will prevent user to exit if the text doesn't match the pattern.
 
Upvote 0
Additionally, if you want, you can add a validation on Exit event, like this:
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Text <> "" Then
    If Not TextBox1.Text Like "##:##" Then
        MsgBox "Wrong input"
        Cancel = True
    End If
End If
End Sub
It will prevent user to exit if the text doesn't match the pattern.
Above and beyond. I was actually talking to a coworker about looking for something like this very thing after i showed off how your first code worked so well.

Due to a series of events starting 3 months ago somehow I got a really big spreadsheet project despite my admission I haven't used excel since probably 7th grade. Learned to add formulas and a bunch of other excel things. Next thing I know my spreadsheet becomes a formal big project and they want forms so people don't have to see the same big scary spreadsheet I do. I've gotten pretty far I think, but these weird coding protections elude me.

I can take a computer apart and put it back together fine probably, but computer coding? Forget it.

I also learned how to make textboxes only accept numbers tonight so that's... exciting lol.

I have a weird question though. So # is obviously the symbol for numbers. What's the symbol for letters in general? I have some textboxes that need info that is in an odd, specific format with numbers, letters and hyphens. I feel like I could pull something together like your hh:mm protection if I knew.
 
Upvote 0
So # is obviously the symbol for numbers. What's the symbol for letters in general?
Please, take a look at these:
I have some textboxes that need info that is in an odd, specific format with numbers, letters and hyphens
I probably can help you if you can give me the specific. Here's a discussion about validating textbox entry with some specific pattern:
Number restriction in VBA UserForm
 
Upvote 0
Please, take a look at these:

I probably can help you if you can give me the specific. Here's a discussion about validating textbox entry with some specific pattern:
Number restriction in VBA UserForm
This is where my coding ignorance kicks in. I feel like I'm close to something, but not quite there. These textboxes have to read like "a-##aaa####-a", a meaning any letter. On an even more advanced level each textbox has to start with a specific letter. One entry will always start with "a-", another with "b-", etc.,
 
Upvote 0
1. Is it "a-" or "A-" or it can be both?
2. Are all the letters must be Uppercase or Lowercase or they can be both?
Technically either is fine, but preference is capital just for ease of reading.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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