How to reference multiple ranges to a 'match' formula I am using

Ritik

New Member
Joined
Aug 28, 2022
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
Hi, I am trying to create a formula (lets say the formula is in cell B15,) that will look at a date that I have in a cell e.g. cell B2 shows "2" which is formulated using =WORKDAY(DATE(A2,A3,0), 1). Where A2 has "2023" and A3 has "1" which signifies the first month of the year. I then want the formula to look at ranges of cells e.g. D29:M29, D33:M33, D35:M35 and D38:M38 (I will be doing multiple formulas, therefore some may have more and some may have less ranges) and if any of these cells contain the value 2, I would like B15 to show up as "holiday".

1663666101477.png


I have attempted to try this and one formula works but only work if it is looking at one range of data.
=IF(ISNUMBER(MATCH(DAY(B2),D25:M25,0)),"Holiday","")
if any cell from D25 to M25 contains 2, Holiday will show.

Another formula that works is =IF(SUMPRODUCT((J28:M28=DAY(F2))+(J31:M31=DAY(F2)))>0,"Holiday",""), but this only works for 2 ranges of data.

If anyone is able to assist me, that would be amazing
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I have a VBA solution that may work for you. Use the function -- IsHoliday -- in a cell. Feed the function with the value sought and up to five ranges to look in for that value. It returns false if the value sought was not found and true if it is. For example this formula looks for 3 in three ranges =IsHoliday(3, I9:J10,L9:L11, M9:O12)

VBA Code:
Function IsHoliday( _
    piValueToFind As Long, _
    prDataRange1 As Range, _
    Optional prDataRange2 As Range, _
    Optional prDataRange3 As Range, _
    Optional prDataRange4 As Range, _
    Optional prDataRange5 As Range _
) As Boolean
    
    Dim rCell As Range
    
    Dim iRangeIndex As Long
    
    Dim rDataRange As Range
    
    IsHoliday = False
    
    For iRangeIndex = 1 To 5

        If iRangeIndex = 1 And Not prDataRange1 Is Nothing _
         Then
            Set rDataRange = prDataRange1
        
        ElseIf iRangeIndex = 2 And Not prDataRange2 Is Nothing _
         Then
            Set rDataRange = prDataRange2

        ElseIf iRangeIndex = 3 And Not prDataRange3 Is Nothing _
         Then
            Set rDataRange = prDataRange3

        ElseIf iRangeIndex = 4 And Not prDataRange4 Is Nothing _
         Then
            Set rDataRange = prDataRange4

        ElseIf iRangeIndex = 5 And Not prDataRange5 Is Nothing _
         Then
            Set rDataRange = prDataRange5
        
        End If

        If Not rDataRange Is Nothing _
         Then

            For Each rCell In rDataRange
            
                If rCell.Value = piValueToFind _
                 Then
                    IsHoliday = True
                    Exit For
                End If

            Next rCell
        
        End If
        
        If IsHoliday Then Exit For
        
        Set rDataRange = Nothing
        
    Next iRangeIndex

End Function
 
Upvote 0
I have a VBA solution that may work for you. Use the function -- IsHoliday -- in a cell. Feed the function with the value sought and up to five ranges to look in for that value. It returns false if the value sought was not found and true if it is. For example this formula looks for 3 in three ranges =IsHoliday(3, I9:J10,L9:L11, M9:O12)

VBA Code:
Function IsHoliday( _
    piValueToFind As Long, _
    prDataRange1 As Range, _
    Optional prDataRange2 As Range, _
    Optional prDataRange3 As Range, _
    Optional prDataRange4 As Range, _
    Optional prDataRange5 As Range _
) As Boolean
   
    Dim rCell As Range
   
    Dim iRangeIndex As Long
   
    Dim rDataRange As Range
   
    IsHoliday = False
   
    For iRangeIndex = 1 To 5

        If iRangeIndex = 1 And Not prDataRange1 Is Nothing _
         Then
            Set rDataRange = prDataRange1
       
        ElseIf iRangeIndex = 2 And Not prDataRange2 Is Nothing _
         Then
            Set rDataRange = prDataRange2

        ElseIf iRangeIndex = 3 And Not prDataRange3 Is Nothing _
         Then
            Set rDataRange = prDataRange3

        ElseIf iRangeIndex = 4 And Not prDataRange4 Is Nothing _
         Then
            Set rDataRange = prDataRange4

        ElseIf iRangeIndex = 5 And Not prDataRange5 Is Nothing _
         Then
            Set rDataRange = prDataRange5
       
        End If

        If Not rDataRange Is Nothing _
         Then

            For Each rCell In rDataRange
           
                If rCell.Value = piValueToFind _
                 Then
                    IsHoliday = True
                    Exit For
                End If

            Next rCell
       
        End If
       
        If IsHoliday Then Exit For
       
        Set rDataRange = Nothing
       
    Next iRangeIndex

End Function
Thank you, this does work! However, instead of the formula having to sought a value, could I reference a cell such as B2, which has a value in it, as I tried this but it was not working. For example, I would want it to be =IsHoliday(B2, I9:J10,L9:L11, M9:O12), where B2 =WORKDAY(DATE(A2,A3,0), 1).
 
Upvote 0
Sorry, I do not understand your issue. I am not sure how you are using Workday function. Might you post real data so I can see what data you are using so, perhaps, I can try to understand what you are trying to accomplish? Maybe show what result you expect from the data as best you can.

You can post a section of a workbook using the excellent MrExcel XL2BB add-in. Details are here: XL2BB - Excel Range to BBCode
 
Upvote 0
DRAFT CCY Holiday Calender - Question.xls
ABCDE
1Jan-23MonTueWedThu
220232345
31
4
5
6AEDTRUEFALSE
7AUD
8CAD
9CHF
10
11
12AED21431
13
14
15
16AED4314
Jan 23
Cell Formulas
RangeFormula
B1:E1B1=CHOOSE(WEEKDAY(B2),"Sun","Mon","Tue","Wed","Thu","Fri","Sat")
B2B2=WORKDAY(DATE(A2,A3,0), 1)
C2C2=WORKDAY(DATE(A2,A3,0), 2)
D2D2=WORKDAY(DATE(A2,A3,0), 3)
E2E2=WORKDAY(DATE(A2,A3,0), 4)
B6B6=IsHoliday(2,B12:H12,B16:H16)
C6C6=IsHoliday(C2,B12:H12,B16:H16)




Here is the sheet I am working with, The code you wrote works well, which is in cell B6, but I am looking to see if instead of typing the number 2 in the formula could I link it to cell B2. such as the example in cell C6.

let me know if you have any more questions
 
Upvote 0
Does this do what you want?

VBA Code:
Function IsHoliday( _
    prValueToFind As Range, _
    prDataRange1 As Range, _
    Optional prDataRange2 As Range, _
    Optional prDataRange3 As Range, _
    Optional prDataRange4 As Range, _
    Optional prDataRange5 As Range _
) As Boolean
    
    Dim rCell As Range
    
    Dim iRangeIndex As Long
    
    Dim rDataRange As Range
    
    IsHoliday = False
    
    For iRangeIndex = 1 To 5

        If iRangeIndex = 1 And Not prDataRange1 Is Nothing _
         Then
            Set rDataRange = prDataRange1
        
        ElseIf iRangeIndex = 2 And Not prDataRange2 Is Nothing _
         Then
            Set rDataRange = prDataRange2

        ElseIf iRangeIndex = 3 And Not prDataRange3 Is Nothing _
         Then
            Set rDataRange = prDataRange3

        ElseIf iRangeIndex = 4 And Not prDataRange4 Is Nothing _
         Then
            Set rDataRange = prDataRange4

        ElseIf iRangeIndex = 5 And Not prDataRange5 Is Nothing _
         Then
            Set rDataRange = prDataRange5
        
        End If

        If Not rDataRange Is Nothing _
         Then

            For Each rCell In rDataRange
            
                If rCell.Value = prValueToFind.Value _
                 Then
                    IsHoliday = True
                    Exit For
                End If

            Next rCell
        
        End If
        
        If IsHoliday Then Exit For
        
        Set rDataRange = Nothing
        
    Next iRangeIndex

End Function
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,856
Members
449,194
Latest member
HellScout

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