loop while not entering a valid time in a cell

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
The user needs to enter a valid timevalue in cell A3. If he slips the finger (invalid time), or if he enters nothing, then I need a loop which either forces him to enter it again, or to leave the current value 09:05.

Thanks in advance.
 

Attachments

  • loop.png
    loop.png
    6.6 KB · Views: 3

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Data Validation would be an alternative solution, but I really need a more user-friendly VBA Loop.
 
Upvote 0
(Right click on tab' name, viewCode, then paste code into)
Not clear what you are trying to do, but here the below code does:
- Input value in cell A3
- As soon as select other cell (or enter), if A3 is invalid time value, a message box pop up with yes/No button:
* If yes: move back to A3 and clear the cell for new input
* If no: add current time in to A3.
If none of above works, try to describe what you want to do, step by step.
VBA Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim timeV As String, info As String
If Target.Address(0, 0) <> "A3" Then
    On Error Resume Next
    timeV = Format(Range("A3").Value, "hh:mm")
    On Error GoTo 0
    If Len(timeV) <> 5 Then
        With Range("A3")
            info = MsgBox(" Invalid time value! Do you want to input again?" & vbLf & vbLf _
                & "Yes (Input again)" & vbLf & vbLf & "No (leave it as current time", vbYesNo)
            Select Case info
                Case vbYes
                    .Select
                    .ClearContents
                Case vbNo
                    .Value = TimeValue(Now)
            End Select
        End With
    End If
End If
End Sub
 
Upvote 0
Thanks Bebo. Whereas your solution is valuable, I have been working on my own a bit, and this is what I made:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim timeV As String
Dim answer As String
Dim kol_A_rng As Range

Set kol_A_rng = Range("A3:A3")

If Not Application.Intersect(Target, kol_A_rng) Is Nothing Then
    Do
        timeV = Format(Target.Value, "hh:mm")
        If IsDate(timeV) Then
            timeV = Format(timeV, "hh:mm")
            'MsgBox ("ok")
        ElseIf timeV = "" Then
            Target.Select
            answer = MsgBox("INVALID ENTRY.  TRY AGAIN ?", vbYesNo)
            Application.Undo
            Exit Sub
        Else
            Target.Select
            answer = MsgBox("INVALID ENTRY.  TRY AGAIN ?", vbYesNo)
            Application.Undo
            Exit Sub
        End If
    Loop While Not (IsDate(timeV))
Else
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,017
Members
448,936
Latest member
almerpogi

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