+ Reply to Thread
Results 1 to 20 of 20

Add VBA to extract hours 2 times (in and out)

  1. #1
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Add VBA to extract hours 2 times (in and out)

    Hello,

    I am trying add to this code I started building and stuck on adding VBA to extract hours from 2 times (columns). The problem is I need to get these hrs, but then -.50. So for example if the hours extracted from 2 times is 7hs I want the result to be 6.5hrs (hence the -.50).

    Here is the attachment and what is highlighted in yellow is where I would like the hours calculate (Columns C:I).

    Any help would be MUCH APPRECIATED!

    Here is the code.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by g1terra; 01-23-2020 at 11:20 PM.

  2. #2
    Forum Contributor
    Join Date
    11-20-2007
    Location
    Felixstowe, England
    MS-Off Ver
    Excel 2013, 2019
    Posts
    217

    Re: Add VBA to extract hours 2 times (in and out)

    I cannot open that file ... some kind of corruption. Can I ask why you aren't doing this via simple formulas?

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Add VBA to extract hours 2 times (in and out)

    I also cannot open the Attachment...it appears to be corrupt...suggest you try to reattach the sample file...
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    Ok sorry about that, lets try this again. I cant use a simple formula because this is an export from a labor program. I was going to use a work around and place a macro button and set users up with a personal file that holds this macro.

    The part I did not add yet needs to find the total hrs scheduled. So 8am - 4pm = 8 hrs. I highlighted the column in yellow where I need it to put the scheduled Hrs.

    Also this code works kind of slow. The export we will be using is only 1 sheet BUT the rows could be the same or could be more stations/hrs.

    Hope that makes sense

    Try this
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    11-20-2007
    Location
    Felixstowe, England
    MS-Off Ver
    Excel 2013, 2019
    Posts
    217

    Re: Add VBA to extract hours 2 times (in and out)

    What rules are to apply? For example, if the Total Hours is less than 6 hours (or less than 5, or 4) ... will the 0.5 hr still need to be subtracted?

    Will you want the result in time format?

    In your code, in a couple of places you have Cells.Select ... which selects every available cell in the sheet ... more than 17 billion cells ... that would slow your code down for any processing related to Selection. I'd use the UsedRange object instead.

  6. #6
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    No rules. I just need the calculation of hrs then minus 30 minutes. The 30 minuets are planned break periods in which they would punch out for

    I here you on cells select. I am a novice with VBA. I’m not too familiar with Used range object. Any help with that would be appreciated

  7. #7
    Forum Contributor
    Join Date
    11-20-2007
    Location
    Felixstowe, England
    MS-Off Ver
    Excel 2013, 2019
    Posts
    217

    Re: Add VBA to extract hours 2 times (in and out)

    Hi,

    time calc code:

    Please Login or Register  to view this content.
    Your other code, tweaked:

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    When i copy and paste that into my code i am having debug issues. Its probably me, like I said I am a novice and having a hard time getting your recommended changes and the time cal added to the original code. Can you help put this together in one code? I have tried lots of variations, i think i am missing something,

    This was the original.
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    Now its getting hung up here with a debug issue. I'm sorry I'm a mess with this..

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    11-20-2007
    Location
    Felixstowe, England
    MS-Off Ver
    Excel 2013, 2019
    Posts
    217

    Re: Add VBA to extract hours 2 times (in and out)

    I'm guessing you had a setup that showed Page breaks or similar, but don't have that now. Remove that code.

  11. #11
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    It was to set page breaks. There is no way I can keep that? It’s kind of important

  12. #12
    Forum Contributor
    Join Date
    11-20-2007
    Location
    Felixstowe, England
    MS-Off Ver
    Excel 2013, 2019
    Posts
    217

    Re: Add VBA to extract hours 2 times (in and out)

    I've never used Page breaks myself ... did that code work before? What happens when you manually set things the way you want, and Record Macro ... what does that code look like?

  13. #13
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    I will delete and and try that piece over again. Can you help me place your modification recommendation and the time calculation into my original above. You posted modifications but I seem to be missing something that wont work. The calc part doesn’t seem to work for me

  14. #14
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    Ok here is the final code. It may be a little slow (like 60 seconds to run) but it works.

    If anyone knows how to make the result the hrs variance highlight yellow please let me know.

    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    11-20-2007
    Location
    Felixstowe, England
    MS-Off Ver
    Excel 2013, 2019
    Posts
    217

    Re: Add VBA to extract hours 2 times (in and out)

    Hi again, here is your code, tweaked, so as to not process millions of cells unnecessarily:

    Please Login or Register  to view this content.
    ... but I don't know what you mean by "make the result the hrs variance highlight yellow" ... can you clarify?

  16. #16
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    That worked!!

    I am adding a part that highlights the hrs over 7.4. I added a call in this macro to do it but it highlights more than what I want for some reason.

    Here is the entire code, you can see the Call Highlight at the end.

    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    Here it is in the sheet. I only want it to highlight the scheduled hrs (when over 7.4).
    Attached Files Attached Files

  18. #18
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    Ok i have everything working except this code only highlights Column I (over 7.4) not Column C as well

    Please Login or Register  to view this content.

  19. #19
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    So the issue here is this part..

    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    06-02-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    235

    Re: Add VBA to extract hours 2 times (in and out)

    I figured that last part of my code.

    Please Login or Register  to view this content.

+ 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. Finding Ship Times based on Hours of Shipper and Hours of Receiver
    By kmsagent in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-01-2019, 02:31 AM
  2. [SOLVED] Formula needed to extract number of hours worked, dependent on shift times
    By heidithecat in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-21-2018, 04:30 AM
  3. Replies: 3
    Last Post: 04-23-2016, 01:42 AM
  4. Replies: 2
    Last Post: 03-02-2013, 10:57 AM
  5. [SOLVED] Formula to work out hours between 2 dates and times.. MINUS Out Of Hours
    By chris.m in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 08-30-2012, 08:11 AM
  6. Calculation of hourly rate times hours times 1.5
    By Newbusinessbod in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-06-2005, 12:50 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