textbox need to be converted in time format

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
VBA Code:
 startTime = Application.WorksheetFunction.Text(Me.TextBox1.Text, "HH:MM AM/PM")
        MsgBox startTime

This might be a simple task but how can I convert a numerical value entered into a textbox to a time value. No matter what I enter in textbox1, it always returns the value of 12:00 AM. But most Youtube or website tutorials usually use the code above minus the messagebox to convert a numerical value entered into the correct time format. Thank you.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
maybe this
VBA Code:
startTime = Format(TextBox1, "HH:MM AM/PM")
 
Upvote 0
No matter what numerical value is entered into this textbox, it always returns "12:00 AM". Do I need to declare startTime as something? Right now, it is not declared. Thank you.
 
Upvote 0
if you are entering a numerical value then try using the TimeSerial function & see if that does what you want

VBA Code:
StartTime = Me.TextBox1.Value
StartTime = TimeSerial(Val(Left(StartTime, 2)), Val(Mid(StartTime, 3, 2)), Val(Right(StartTime, 2)))
Me.TextBox1 = Format(StartTime, "HH:MM AM/PM")

Dave
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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