+ Reply to Thread
Results 1 to 9 of 9

VBA to Prevent Timekeeping Values from being Negative

  1. #1
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    VBA to Prevent Timekeeping Values from being Negative

    I'm trying to make a time-keeping worksheet with clock-in and clock-out times (example attached). I'm using vba to alert the user if the clock out time is earlier than the start time. My code works...sort of

    What I'd really like to add to this is a loop that won't let you move on until you've fixed the problem. In other words, it won't let any values in column H remain negative. Does that make sense?


    Please Login or Register  to view this content.

    Thanks!!
    Attached Files Attached Files

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

    Re: VBA to Prevent Timekeeping Values from being Negative

    I suggest to use the Data Validation feature.

    On Sheet1, select E3:E10 (or the end-time data cells)

    On the menu select Data\ Validation
    Allow: Time
    Data: greater than
    Start time =$C3
    On the Error alert tab, configure your message.

    The user won't be allowed to enter a time less than the start times.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    Re: VBA to Prevent Timekeeping Values from being Negative

    AlphaFrog, that accomplishes part of what I'm wanting. But with that DV rule, I can't have a dropdown list of times. My users would have to hard enter the time, and I don't want that.

    I know you can combine DV rules into a custom formula, but I don't know of a way to combine a formula with a list.

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

    Re: VBA to Prevent Timekeeping Values from being Negative

    This DV formula will create a DV Dropdown list where the times in the list are all after the start time in column C.

    =OFFSET($M$1,MATCH($C3,$M:$M,1),0,COUNT($M:$M)-MATCH($C3,$M:$M,1))

    The list of all times in order from earliest to latest is in column M starting at M1 .

  5. #5
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    Re: VBA to Prevent Timekeeping Values from being Negative

    Alpha, I'm a little confused, do I put that formula in the DV custom formula? And can I use the same list of times that my first column is referring to?

    Either way it's not working. The clock-out column doesn't have a dropdown and the error is buggy.

    Is there a way to do this with VBA?

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

    Re: VBA to Prevent Timekeeping Values from being Negative

    Quote Originally Posted by phelbin View Post
    Alpha, I'm a little confused, do I put that formula in the DV custom formula? And can I use the same list of times that my first column is referring to?
    Yes and Yes
    What is the range that has the list of times? Substitute that range for M:M is the formula.

    Either way it's not working. The clock-out column doesn't have a dropdown and the error is buggy
    Perhaps the example workbook could have been more representative of your actual workbook. There were no DV lists in the example.

    Is there a way to do this with VBA?
    Certainly you could use VBA, but I think this is a more elegant solution.

  7. #7
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    Re: VBA to Prevent Timekeeping Values from being Negative

    The sample file does have a DV list...it's on Sheet2. And columns C and E are both referring to it.

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

    Re: VBA to Prevent Timekeeping Values from being Negative

    Quote Originally Posted by phelbin View Post
    The sample file does have a DV list...it's on Sheet2. And columns C and E are both referring to it.
    I just downloaded the example workbook again and neither sheets 1 or 2 have DV lists.

  9. #9
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    Re: VBA to Prevent Timekeeping Values from being Negative

    In my example workbook, Sheet1 C3:C10, when I bring up the DV dialog box, it has list with the source 'Sheet2'!A1:A97.

    As far as I can tell it's working because those cells have dropdowns with the data on Sheet2.

    Perhaps there's something I don't know about how DV works? Does a list have to be designated as such?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. NoobQ: Rank function treats negative values as positive values. Help!
    By lutonoodles in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-12-2013, 02:10 PM
  2. Replies: 8
    Last Post: 07-28-2012, 03:22 PM
  3. Prevent Formula Returning Negative Result
    By Manic in forum Excel General
    Replies: 2
    Last Post: 11-09-2011, 11:27 AM
  4. Timekeeping
    By cbdeajr in forum Excel General
    Replies: 8
    Last Post: 07-26-2011, 07:00 AM
  5. timekeeping, merge & sort non-adjacent cells
    By BeatBama in forum Excel General
    Replies: 1
    Last Post: 01-14-2011, 12:16 PM
  6. Timekeeping
    By dyscjocki in forum Excel General
    Replies: 5
    Last Post: 09-05-2009, 04:29 PM
  7. Prevent from entering negative amounts
    By oteixeira in forum Excel General
    Replies: 1
    Last Post: 12-23-2008, 05:47 PM
  8. Replies: 4
    Last Post: 09-26-2005, 06:05 PM

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