Input box to enter hours and minutes

brianfosterblack

Active Member
Joined
Nov 1, 2011
Messages
251
I please need some help with an Inputbox. The user needs to enter the hours and minutes (or minutes only) in the Activecell for the time spent on a task
VBA Code:
Activecell.value = InputBox ("Enter the hours and minutes spent on the task in this format 1:30 or 0:45")
The entry needs to be verified as being in the right format.

Can anyone please help with this
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi,
try

VBA Code:
Sub GetTime()
    Dim Entry           As Variant
    Dim ValidTimeEntry  As Boolean
    Dim strDefault      As String
    
    Do
        Entry = InputBox("Enter the hours And minutes spent On the task in this format 1:30 Or 0:45", "Enter Time", strDefault)
        'cancel pressed
        If StrPtr(Entry) = 0 Then Exit Sub
        On Error Resume Next
        ValidTimeEntry = IsDate(TimeValue(Entry))
        On Error GoTo 0
        strDefault = "Invalid Time Entry"
    Loop Until ValidTimeEntry
    
    Entry = TimeValue(Entry)
    
    
    ' rest of code
    
    
End Sub

Dave
 
Upvote 0
Solution
Hi,
try

VBA Code:
Sub GetTime()
    Dim Entry           As Variant
    Dim ValidTimeEntry  As Boolean
    Dim strDefault      As String
   
    Do
        Entry = InputBox("Enter the hours And minutes spent On the task in this format 1:30 Or 0:45", "Enter Time", strDefault)
        'cancel pressed
        If StrPtr(Entry) = 0 Then Exit Sub
        On Error Resume Next
        ValidTimeEntry = IsDate(TimeValue(Entry))
        On Error GoTo 0
        strDefault = "Invalid Time Entry"
    Loop Until ValidTimeEntry
   
    Entry = TimeValue(Entry)
   
   
    ' rest of code
   
   
End Sub

Dave
Hi Dave, thanks for the reply but this code is not working. It enters nothing in the cell even though the code is running through. The last line of code I am entering before plugging in your code is
VBA Code:
Activecell.offset (0,-1).Select
Next line is your code.
I am doing this to select the cell whereas before I had the code as
VBA Code:
Activecell.offset (0,-1).value = Inpubox....
 
Upvote 0
Solution provides validation of entry you requested & I added at the end, this code
& comment

VBA Code:
Entry = TimeValue(Entry)
    
    
    ' rest of code

where you would use the variable Entry as required in your code

for instance

VBA Code:
ActiveCell.Value = Entry

Hope Helpful

Dave
 
Upvote 0
Solution provides validation of entry you requested & I added at the end, this code
& comment

VBA Code:
Entry = TimeValue(Entry)
   
   
    ' rest of code

where you would use the variable Entry as required in your code

for instance

VBA Code:
ActiveCell.Value = Entry

Hope Helpful

Dave
Hi Dave,
This works perfectly. Thank you so much for your assistance.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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