+ Reply to Thread
Results 1 to 4 of 4

VBA to check for a missing date

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-14-2012
    Location
    Tucson, Arizona, USA
    MS-Off Ver
    Office 2010
    Posts
    105

    VBA to check for a missing date

    What I have is a userform that displays the last date data was entered in one of its many textboxes and what I'm trying to prevent is the next date from being accidentally skipped. I have something similar on another userform that checks the last time an entry was made and that the next entry made is 2 hours after that.

    I figure it's got to be something simple that I'm missing or not seeing. I've put the coding below for each userform and hope one of you can help me figure this out.

    This is the coding I can't get to work right
    Midnights WorkSheet <--All the coding here does not work
    'checks any date is not reentered <--This works fine
    If WorksheetFunction.CountIf(ws.Columns(2), Trim(Me.cboDate.Value)) > 0 Then
        MsgBox "Sorry, " & Trim(Me.cboDate.Value) & " exists already", vbExclamation, "Invalid Date Entry"
        MsgBox "If you are correcting an error," & vbFl & "Please close this form and select Edit instead", vbExclamation, "Fix The Date"
        Exit Sub
    End If
    
    'checks that there is only 1 day between each entry <--This does not work properly, runs every time
    If cboDate.Value <> Format(DateAdd("d", 1, txtMidDate2.Value), "dd-mmm-yyyy") Then
      MsgBox "The " & Format(DateAdd("d", 1, txtLastDate.Value), "dd-mmm-yyyy") & " Midnight Report is missing!" & vbLf & "Please enter it first", vbExclamation, "Missing An Entry"
      Exit Sub
    End If
    This is the coding that works for the second userform
    Thermals WorkSheet <--All the coding here works fine
    'checks any date and time is not reentered
    If WorksheetFunction.CountIfs(ws.Columns(2), Trim(Me.cboDate.Value), ws.Columns(3), Trim(Me.cboTime.Value)) > 0 Then
        MsgBox "Sorry, " & Trim(Me.cboDate.Value) & " at " & Trim(Me.cboTime.Value) & " exists already", vbExclamation, "Invalid Time Entry"
        MsgBox "If you are correcting an error," & vbLf & "Please close this form and select Edit instead", vbExclamation, "Fix The Time"
        Exit Sub
    End If
    
    'checks that there is only 2 hours between each entry
    If cboTime.Value <> Format(DateAdd("h", 2, txtLTime.Value), "hh:mm") Then
      MsgBox "The " & Format(DateAdd("h", 2, txtLTime.Value), "hh:mm") & " Thermals are missing", vbExclamation, "Missing An Entry"
      Exit Sub
    End If
    Last edited by LoneWolf3574; 06-18-2013 at 10:33 PM.

  2. #2
    Forum Contributor
    Join Date
    09-14-2012
    Location
    Tucson, Arizona, USA
    MS-Off Ver
    Office 2010
    Posts
    105

    Re: VBA to check for a missing date

    DOH, I just figured it out . I had to do with the formating of the two values being compared.

    I had an idea that I thought was a long shot and changed the following line
    If cboDate.Value <> Format(DateAdd("d", 1, txtMidDate2.Value), "dd-mmm-yyyy") Then
    to this instead and it worked.
    If Format(DateAdd("d", 0, cboMidDate.Value), "dd-mmm-yyyy") <> Format(DateAdd("d", 1, txtMidDate2.Value), "dd-mmm-yyyy") Then

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: VBA to check for a missing date

    This may work as well...
    If CDate(cboMidDate.Value) <> CDate(txtMidDate2.Value) + 1 Then

  4. #4
    Forum Contributor
    Join Date
    09-14-2012
    Location
    Tucson, Arizona, USA
    MS-Off Ver
    Office 2010
    Posts
    105

    Re: VBA to check for a missing date

    That works to and it's much shorter. You're just full of good ideas AlphaFrog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1