Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 8th, 2014, 07:32 PM
Authorized User
 
Join Date: Jan 2012
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with code

Code:
Private Sub txtTestTimeInput_Exit(ByVal Cancel As MSForms.ReturnBoolean)

With txtTestTimeInput
    txtTestTimeInput.Text = Format(txtTestTimeInput.Value, "##:##")
End With
    
Dim strMsge As String
Dim arrSplit

dteDate = 0
If Trim(Len(txtTestTimeInput)) = 0 Then Exit Sub  'Required to allow deletion of time

arrSplit = Split(txtTestTimeInput.Value, ":")

If UBound(arrSplit) <> 1 Then  'Does not have colon separator
    strMsge = "Incorrect time format. Use colon as separator."
    GoTo msgLabel
End If

If arrSplit(0) < 0 Or arrSplit(0) > 23 Then
    strMsge = "Hours must be between 0 and 23."
    GoTo msgLabel
End If

If arrSplit(1) < 0 Or arrSplit(1) > 59 Then
    strMsge = "Minutes must be between 0 and 59."
    GoTo msgLabel
End If

dteTime = TimeSerial(arrSplit(0), arrSplit(1), 0)

txtTestTimeInput.Value = Format(dteTime, "hh:mm")    'Re-write time in correct format.

Exit Sub    'If gets to here then time OK so exit here

msgLabel:
Cancel = True   'Cancels the update and cursor remains in the TextBox control
MsgBox strMsge & vbCrLf & "Re-enter time correctly in hhmm format."
txtTestTimeInput.Text = ""
End Sub
I found this above code on another forum and the only problem that I can see so far is if the user enters the time between midnight(00:00) and 1am(01:00). It will execute this part of the code. In other words when the user enters "00" for midnight the program will display the below message. What could I add to the current code to fix this problem. Thanks.
Code:
If arrSplit(0) < 0 Or arrSplit(0) > 23 Then
    strMsge = "Hours must be between 0 and 23."
    GoTo msgLabel
End If





Similar Threads
Thread Thread Starter Forum Replies Last Post
Code to save html page source code? Jaymond Flurrie Access VBA 2 July 2nd, 2019 06:04 PM
Function to convert CUSIP code to ISIN code ... cusipconv() amkh8857 Excel VBA 1 March 28th, 2013 02:37 AM
Urgent:hard disk serial code and vb code ivanlaw Pro VB 6 0 July 25th, 2007 04:05 AM
VB: .Exe file, serial code and activation code ivanlaw Pro VB 6 8 July 6th, 2007 05:44 AM
Writing Client Side Script from Code-Behind code sajid_pk Classic ASP Databases 1 January 18th, 2005 12:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.