+ Reply to Thread
Results 1 to 4 of 4

If condition satisfied but ignored

Hybrid View

  1. #1
    Registered User
    Join Date
    07-08-2019
    Location
    perth
    MS-Off Ver
    365
    Posts
    2

    Unhappy If condition satisfied but ignored

    OK this is annoying me.
    The If condition is satisfied but jumps to the next one where the (O_Hrs + Time_Acc >8) when in fact it satisfies the (O_Hrs + Time_Acc <=8)

    What on earth am i missing.

     If (Time_Acc < 8) And (O_Hrs + Time_Acc <= 8) And (OT_Type = "New") Then
                                    MYOB_Pre_Export.Cells(X, "A").Copy MYOB_Export.Cells(Y, "A")
                                    MYOB_Pre_Export.Cells(X, "B").Copy MYOB_Export.Cells(Y, "B")
                                    MYOB_Pre_Export.Cells(X, "C").Copy MYOB_Export.Cells(Y, "C")
                                    MYOB_Pre_Export.Cells(X, "G").Copy MYOB_Export.Cells(Y, "G")
                                    If MYOB_Pre_Export.Cells(X, "G") = "A - ANNUAL LEAVE" Then 'checks if its annual leave hours
                                         MYOB_Export.Cells(Y, "D") = ".Holiday Pay Hourly"
                                    Else
                                        If MYOB_Pre_Export.Cells(X, "G") = "A - SICK LEAVE" Then 'checks if its sick leave hours
                                        MYOB_Export.Cells(Y, "D") = ".Sick Pay Hourly"
                                        Else
                                         MYOB_Export.Cells(Y, "D") = "..Base Hourly"
                                        End If
                                        End If
                                                                          
                                        If (lunch_break = 0) And (Time_Acc + O_Hrs > 5) Then 'checks to see if lunch break flag is not set and the worked time greater than 5 hours
                                        C_Hrs = (O_Hrs - 0.5) 'deducts luch break
                                        lunch_break = 1
                                        Else
                                        C_Hrs = O_Hrs 'no lunch break deducted
                                        End If
                                        MYOB_Export.Cells(Y, "F") = C_Hrs
                                        Y = Y + 1
                                        
                                    Else
                                    If (Time_Acc < 8) And (O_Hrs + Time_Acc > 8) And (O_Hrs + Time_Acc < 10) And (OT_Type = "New") Then
                                    MYOB_Pre_Export.Cells(X, "A").Copy MYOB_Export.Cells(Y, "A")
                                    MYOB_Pre_Export.Cells(X, "B").Copy MYOB_Export.Cells(Y, "B")
                                    MYOB_Pre_Export.Cells(X, "C").Copy MYOB_Export.Cells(Y, "C")
                                    MYOB_Pre_Export.Cells(X, "G").Copy MYOB_Export.Cells(Y, "G")
                                                              MYOB_Export.Cells(Y, "D") = "..Base Hourly"
                                                              
                                    If (lunch_break = 0) And (Time_Acc + O_Hrs > 5) Then 'checks to see if lunch break flag is not set and the worked time greater than 5 hours
                                    C_Hrs = ((8 - Time_Acc) - 0.5) 'deducts lunch break
                                    lunch_break = 1
                                    Else
                                    C_Hrs = (8 - Time_Acc)
                                    End If


    VBA If condition fail.png
    Last edited by remkin76; 07-08-2019 at 11:48 PM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,044

    Re: If condition satisfied but ignored

    Quote Originally Posted by remkin76 View Post
    OK this is annoying me.
    The If condition is satisfied but jumps to the next one where the (O_Hrs + Time_Acc >8) when in fact it satisfies the (O_Hrs + Time_Acc <=8)
    Are you sure it is being satisfied? Hard to say for sure without seeing any data, but if you are testing a REAL time against 8, then this is probably where you are getting mixed up.

    What you need to understand about dates and times in excel is…

    a date is just a number representing the number of days passed since 1/1/900...and then formatted in a way that we recognize as a date. So, for instance, today (Tue 09 Jul 2019) is actually 43655

    Time is actually a decimal part of 1 (day), so 06:00 AM is 0.25, 12 noon is 0.5 and 18:00 (or 6 PM) is 0.75

    So, having said all that, 8 (am) is actually 0.333 and 8 (pm) is really 0.666
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    07-08-2019
    Location
    perth
    MS-Off Ver
    365
    Posts
    2

    Re: If condition satisfied but ignored

    Thanks Ford,
    Is definitely satisfied as the values are Dim as Double and the rest of the code works with these values. If you look at the picture I posted it has the watch values. The excel cells are definitely not formatted as dates. Can you see the watch values in the picture. Whats actually worse for me is that the next condition which is greater than 8 has been satisfied.

    I have several other sheets which are converting the values to numbers, the dates are not used as reference in formulas I use some trickery to determine if the dates are the same or not an convert the times to number specifically for calculations. The timekeeping software also rounds to the nearest 0.25 so we cant have a value of anything other than .25 .50 or .75 so we inherently cant be that close to 8

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,044

    Re: If condition satisfied but ignored

    Pics are often hard to make out. I suggest that you upload a small (clean) sample workbook (not a pic) of what you are working with, and what your expected outcome would look like.

+ 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. Replies: 7
    Last Post: 06-01-2015, 06:05 PM
  2. Extract values if condition is satisfied
    By bjnockle in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 08-26-2014, 08:39 AM
  3. Replies: 2
    Last Post: 08-01-2011, 03:22 PM
  4. Replies: 8
    Last Post: 05-06-2011, 08:25 AM
  5. [SOLVED] Deleting columns if condition is satisfied
    By kent-dk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-15-2006, 06:25 AM
  6. Copy sheets only if condition is satisfied (mat)
    By matthias in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-10-2006, 01:10 PM
  7. Replies: 0
    Last Post: 01-11-2006, 07:31 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