Forum Discussion

Saboor16's avatar
Saboor16
Copper Contributor
Apr 19, 2024

Help with VBA Functions

I am using excel for this Data looks like this
               Monday    Tuesday    Wednesday    Thursday    Friday    Staurday    Sunday
               6/04/24    6/05/24       6/06/24        6/07/24    6/08/24   6/09/24    6/10/24
Zachary      12              12                0                  0               12            8            8
Nipun           0              12                0                   0               12            12             0
Juno            12               12               0                   0               8              12           12
Ankita          12              12              12                   0            12              12           12

I want to create a function using VBA.
The function should like this
=FunctionName(B4:H4,B1:H1)
So I want the function to first check the cells and see if any of them are yellow in color RGB(255,255,0) and have 8 written inside the cell. Than for those cells check the corresponding days names. If its Saturday or Sunday than ignore. But if its any of the day from Monday-Friday than I want it to add the hours for the whole week. So for Juno the output should be 32. and for the rest the output should be 0.

1 Reply

  • NikolinoDE's avatar
    NikolinoDE
    Platinum Contributor

    Saboor16 

    You can achieve this with a custom VBA function. Here's a VBA code that creates a custom function to implement the logic you described:

    Vba code is untested, please backup your file first.

    Function SumYellowHours(hoursRange As Range, daysRange As Range) As Integer
        Dim cell As Range
        Dim totalHours As Integer
        Dim i As Integer
        
        totalHours = 0
        
        ' Loop through each cell in the range
        For i = 1 To hoursRange.Cells.Count
            Set cell = hoursRange.Cells(i)
            
            ' Check if the cell is yellow and contains 8
            If cell.Interior.Color = RGB(255, 255, 0) And cell.Value = 8 Then
                ' Check if the corresponding day is Monday-Friday
                If Weekday(daysRange.Cells(i).Value, vbMonday) <= 5 Then
                    ' Add hours to total
                    totalHours = totalHours + cell.Value
                End If
            End If
        Next i
        
        SumYellowHours = totalHours
    End Function

    To use this function, follow these steps:

    1. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
    2. Go to Insert > Module to insert a new module.
    3. Copy and paste the above VBA code into the module window.
    4. Close the VBA editor.
    5. In your Excel worksheet, enter the formula =SumYellowHours(B4:H4, B1:H1) in the cell where you want the result to appear.
    6. Press Enter to calculate the result.

    This function will loop through each cell in the specified range (B4:H4 for hours and B1:H1 for days), check if the cell is yellow and contains 8, and if the corresponding day is Monday-Friday. If all conditions are met, it adds the hours to the total. Finally, it returns the total hours as the result of the function.Formularbeginn The text, steps and code were created with the help of AI.

     

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

    Was the answer useful? Mark as best response and Like it!

    This will help all forum participants.

Resources