Excel VBA IF Then Else Loop Output Not Returning Different Statements Based on Criteria

SSap

New Member
Joined
Apr 23, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Please help! I'm fairly new to working with macros and I am having trouble figuring out how to get my macro to loop within an If Then Else so that it will run a column and check if 'Hours' are either:
  1. Greater than 'AvailHours' Then the output for 'Reason' would be "Not enough hours earned"
  2. 'Hours' < 4 Then the output for 'Reason' would be "Pay for 4 hours"
  3. 'Hours' >= 4 Then the output for 'Reason' would be "Pay for 8 hours"
  4. If the output for 'Reason' is "Not enough hours earned" then I need that row to move to the bottom
I have tried different macros and I either just get only one of the output statements all the way down the Reason column or errors in my macro not allowing it to complete. I am working in sheet EXCEPTIONS in a workbook with other sheets.

Is there a better way to write my macro to get the desired results that I need....

This is what I have:

'Check if enough Avail Sick Time Hours and Round Hours Worked
Range("EXCEPTIONS[Hours]").Select

For Each cell In Range("EXCEPTIONS[Hours]")
If Value < "EXCEPTIONS[AvailHours]" Then
Range("EXCEPTIONS[Reason]").Value = "Not enough hours earned"
ElseIf Value < 4 Then
Range("EXCEPTIONS[Reason]").Value = "Pay for 4 hours"
ElseIf Value >= 4 Then
Range("EXCEPTIONS[Reason]").Value = "Pay for 8 hours"
End If

Next cell



Output Reason I Get:

Output I Get




This is what I am trying to get it to do for the Reason:

Output I Need
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Seems you're logic is not setting it to the cell value and only your first condition is being seen and your reference is off too . . .
Maybe something like:
VBA Code:
Sub mre_1235604()
'Check if enough Avail Sick Time Hours and Round Hours Worked

    ' See below Range("EXCEPTIONS[Hours]").Select ' If you are using a Table style reference you should use Sheet("SheetName").ListObjects("EXCEPTIONS").ListColumns("Hours").Select
    
    For Each tblCell In Sheets("MrExcel-1235604").ListObjects("EXCEPTIONS").ListColumns("Hours").DataBodyRange
        With tblCell  'Not sure your logic is clear here; guessing at intent . . .  Also, tblCell.Row -1 to skip header
            If .Value < Sheets("MrExcel-1235604").ListObjects("EXCEPTIONS").ListColumns("AvailHours").DataBodyRange(tblCell.Row - 1).Value Then ' This should only initial check to see if there are enough hours before determining amount to pay
                If .Value >= 4 Then Sheets("MrExcel-1235604").ListObjects("EXCEPTIONS").ListColumns("Reason").DataBodyRange(tblCell.Row - 1) = "Pay for 8 hours"
                If .Value < 4 Then Sheets("MrExcel-1235604").ListObjects("EXCEPTIONS").ListColumns("Reason").DataBodyRange(tblCell.Row - 1) = "Pay for 4 hours"
            Else
                Sheets("MrExcel-1235604").ListObjects("EXCEPTIONS").ListColumns("Reason").DataBodyRange(tblCell.Row - 1) = "Not enough hours earned"
            End If
        End With
    Next tblCell
End Sub

Book2
ABCDEFGHIJK
1EmpIDNameJobHoursRatePayReasonBudget CodeBlankAvailHoursJob Code
22342344328Pay for 8 hours53.153
32343242347.5Pay for 8 hours9.33
42341232144Not enough hours earned3.466
52343242348Not enough hours earned6.8
62341232144Pay for 8 hours6.8
71234567893.5Pay for 4 hours5
MrExcel-1235604
 
Upvote 0
Thanks, I was able to fix my code after reviewing your suggestion. This is what I ended up with.

VBA Code:
'Check if enough Avail Sick Time Hours and Round Hours Worked
 Dim Cell As Range

    For Each Cell In Range("EXCEPTIONS[Hours]")
        With Range("EXCEPTIONS[Reason]").Cells(Cell.Row - 1)
            If Cell > Range("EXCEPTIONS[AvailHours]").Cells(Cell.Row - 1) Then
                .Value = "Not enough hours earned"
            ElseIf Cell < 4 Then
                 .Value = "Pay for 4 hours"
            ElseIf Cell >= 4 Then
                 .Value = "Pay for 8 hours"
            End If
         End With
    Next Cell
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top