+ Reply to Thread
Results 1 to 12 of 12

Help counting hours in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Question Help counting hours in VBA

    Hi all,
    I've found some code which i'm using for a staff scheduling system. It can turn strings such as 12pm-5pm into time and then work out the hours which is great, it will also add them up for any given working week. However, what I really need is some more code that will count down the rows rather than the columns and give me a total employee count per hour.
    For instance, I have 3 employees - A, B, and C.
    Name Monday
    A 11am - 3pm
    B 12pm - 6pm
    C 12pm - 9pm
    What I'd like to be able to do for any given day is have a count for how many hours are being spent, per hour. So using the above table it would count 1 hour for 11am-12pm, 3 hours from 12pm -1pm, etc.

    Please see the current code below, if you can see a way I can extrapolate the data I need from the data already created then I'd be really grateful, my VBA skills are very limited as I only delve into it when standard formulas won't cut it. I appreciate any help that you guys and gals can give.

    Function Hours(rng As Range)
    
        Hours = GetCellHours(rng)
    
    End Function
    
    Function Pay(rng As Range)
        
        Pay = rng(3) * rng(5)
        
    End Function
    
    
    
    Function GetCellHours(rng As Range)
    
        Dim cell As Range
        Dim Hours As Double
        
        For Each cell In rng
            If cell.Column >= 7 Then
            
                If cell.Text <> "" Then
                    Dim lines() As String
                    Dim line
                    lines = Split(Replace(cell.Text, "/", vbLf), vbLf)
                    For Each line In lines
                        Hours = Hours + GetLineHours(CStr(line))
                    Next line
                Else
                    If rng.Worksheet.Cells(5, cell.Column).Text = "" Then
                        GoTo Finish
                    End If
                End If
            
            End If
        Next cell
        
        
    Finish:
        
        If Hours < 0 Then Hours = 0
        
        GetCellHours = Hours
    
    End Function
    
    Function GetLineHours(str As String)
    
        Dim Hours As Double
        
        
        On Error GoTo NoHours
        
        If str = Null Then GoTo NoHours
        If str = "" Then GoTo NoHours
        str = Trim(str)
        If str = "" Then GoTo NoHours
      
        Dim words() As String
        
        If InStr(str, "-") Then
            
            words = Split(str, "-")
            
            If UBound(words) + 1 = 2 Then
            
                Dim start As Date
                start = CDate(words(0))
                
                Dim Finish As Date
                Finish = CDate(words(1))
                
                Hours = 24 * (Finish - start)
            
                If Hours <= 0 Then
                    Hours = Hours + 24
                End If
            
            End If
            
        ElseIf InStr(str, " ") Then
        
            words = Split(str)
            
            If UBound(words) + 1 = 3 Then
                If words(2) = "break" Then
                    Dim amount As Double
                    amount = CDbl(words(0))
                    Dim interval As String
                    interval = words(1)
                    
                    If interval = "minute" Or interval = "min" Or interval = "mins" Then
                        amount = (amount / 60)
                    ElseIf interval = "hours" Or interval = "hour" Or interval = "hr" Or interval = "hrs" Then
                    Else
                        amount = 0
                    End If
                    
                    Hours = -amount
                End If
            End If
            
            If UBound(words) + 1 = 2 Then
                If words(1) = "hours" Or words(1) = "hour" Then
            
                    Hours = CDbl(words(0))
    
                End If
            End If
            
        End If
        
        GoTo NoError
    
    NoHours:
        Hours = 0
    
    NoError:
        GetLineHours = Hours
        
    End Function
    Thank you in advance!
    ~Wabbit

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Help counting hours in VBA

    Hello WarWabbit,

    Welcome to the Forum!

    Do the times start in column "G"?

    What is the starting row?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    Hi Leith,
    Yes, the times start in the G column - so run G-M (Mon-Sun).
    The starting row is 6.

    Thanks,
    ~Wabbit

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Help counting hours in VBA

    Hello WarWabbit,

    The macro will replace the times in columns G - M with the hours, correct?

    So, 11am - 3pm would become 5 or would it be 5 hours?

  5. #5
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    Hi,
    It won't actually change the cell value, it will leave them as a string so the user can see the shift that has been input, the vba code captures the daily hours which I can then output as a day or as the whole week, which is what I need. It's the ability to strip the hours of a day into the appropriate times. So with a 12pm - 3pm shift I need to be able to output that 1 hour has been worked at 12pm, 1pm and 2pm. If it's easier I can link you the file if there is a way to do that?

    Cheers,
    ~Wabbit
    Last edited by WarWabbit; 12-29-2016 at 01:47 PM.

  6. #6
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    Posted twice, please delete.
    Last edited by WarWabbit; 12-29-2016 at 01:05 PM.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Help counting hours in VBA

    You can post a copy of the workbook here at the forum.

    To Attach a File:

    1. Scroll down to the window below your post Additional Options
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit.

  8. #8
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    Thanks,
    Please ignore everything from the O column onwards, it's not applicable to the general workflow and is the next stage of reporting i'm working on.

    Cheers,
    ~Wabbit
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    I'm not sure of the scope of Vars in VBA, is it possible to create a countif function capturing the 'Hours' as it goes through the GetLineHours function?

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Help counting hours in VBA

    Hello WarWabbit,

    I rewrote the UDF. It is much shorter and recalculates whenever a change is made to a dependent cell. All of the =Hours() UDF calls have been replaced with the new UDF call =CalculateHours()

    Module2 UDF Macro Code

    Function CalculateHours(ByRef Rng As Range)
    
        Dim Cell    As Range
        Dim Hours   As Integer
        Dim j       As Integer
        Dim k       As Integer
        Dim n       As Integer
        Dim Text    As String
        Dim x       As Variant
        Dim y       As Variant
        
            Application.Volatile
            
            For Each Cell In Rng
                n = 1
                Text = Replace(Cell.Value, "/", "")
                Do
                    j = InStr(n + 1, Text, "-")
                    k = InStr(j + 1, Text, "m", vbTextCompare)
                    If j > 0 And k > 0 Then
                        x = Mid(Text, n, k - j + 1)
                        y = Mid(Text, j + 1, k - j)
                        If LCase(x) Like "*pm*" Then x = (Val(x) Mod 12) + 12 Else x = Val(x)
                        If LCase(y) Like "*pm*" Then y = (Val(y) Mod 12) + 12 Else y = Val(y)
                        If y > x Then Hours = Hours + (y - x) Else Hours = Hours + ((24 - x) + y)
                        n = k + 1
                    Else
                        Exit Do
                    End If
                Loop
            Next Cell
            
            CalculateHours = Hours
            
    End Function
    Attached Files Attached Files

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Help counting hours in VBA

    Hello WarWabbit,

    I cleaned the code up a little. This removes some unnecessary redundancy.

    Improved UDF Code
    Function CalculateHours(ByRef Rng As Range)
    
        Dim Cell    As Range
        Dim Hours   As Integer
        Dim j       As Integer
        Dim k       As Integer
        Dim n       As Integer
        Dim Text    As String
        Dim x       As Variant
        Dim y       As Variant
        
            Application.Volatile
            
            For Each Cell In Rng
                n = 1
                Text = LCase(Replace(Cell.Value, "/", ""))
                Do
                    j = InStr(n + 1, Text, "-")
                    k = InStr(j + 1, Text, "m")
                    If j > 0 And k > 0 Then
                        x = Mid(Text, n, k - j + 1)
                        y = Mid(Text, j + 1, k - j)
                        If x Like "*pm*" Then x = (Val(x) Mod 12) + 12 Else x = Val(x)
                        If y Like "*pm*" Then y = (Val(y) Mod 12) + 12 Else y = Val(y)
                        If y > x Then Hours = Hours + (y - x) Else Hours = Hours + ((24 - x) + y)
                        n = k + 1
                    Else
                        Exit Do
                    End If
                Loop
            Next Cell
            
            CalculateHours = Hours
            
    End Function

  12. #12
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    Thank you for improving the code. Did you have any luck with it being able to split an employee's hours into individual hours?

    Cheers,
    ~Wabbit

+ 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. Elapsed working hours, without counting weekends or non-working hours
    By ebkiwi in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-16-2013, 04:18 PM
  2. Counting difference between hours if outside of business hours
    By joytech22 in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 07-15-2013, 05:03 AM
  3. Excel 2007 : counting hours
    By stonefacedolly in forum Excel General
    Replies: 10
    Last Post: 03-17-2010, 05:27 PM
  4. Counting Hours
    By Don Juan in forum Excel General
    Replies: 4
    Last Post: 11-17-2009, 01:16 PM
  5. Counting hours on a timesheet
    By tazmanian24 in forum Excel General
    Replies: 2
    Last Post: 10-15-2009, 10:05 AM
  6. Counting hours
    By Analyst99 in forum Excel General
    Replies: 3
    Last Post: 10-26-2006, 07:20 PM
  7. [SOLVED] Counting hours
    By vanilla_bean_orange via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-17-2006, 07:15 AM

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