VBA to complete Index/Match with 2 criteria

Drrellik

Well-known Member
Joined
Apr 29, 2013
Messages
842
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
Time by week Master.xlsm
ABCDE
1ADAMS ANTONIO8/26/202435202413.5
2ADAMS ANTONIO8/27/202435202414.93
3ADAMS ANTONIO8/28/202435202413.77
4ADAMS ANTONIO8/29/202435202414.52
5ADAMS ANTONIO8/30/202435202413.33
6ADAMS ANTONIOWeek 35 Total35202470.05
7ADAMS KEVIN8/27/202435202414.93
8ADAMS KEVIN8/28/202435202413.77
9ADAMS KEVIN8/29/202435202414.52
10ADAMS KEVIN8/30/202435202413.33
11ADAMS KEVINWeek 35 Total35202456.55
Sheet2


This data is located on sheet2

Time by week Master.xlsm
BCDEFGHIJKL
1EMPLOYEE NAMEYEAR - & WEEK -2024348/18/20248/19/20248/20/20248/21/20248/22/20248/23/20248/24/2024
2Ansari Abdus-Sabur Q.
3Argroe Ryant
4Brantley Jonathan
Payroll Week Time
Cell Formulas
RangeFormula
F1F1=DATE(D1,1,1)+(E1-1)*7-WEEKDAY(DATE(D1,1,1),2)
G1:L1G1=F1+1


This data is located on Payroll Week Time

the VBA I am using did not error out and seemed to loop through but it did not populate my Payroll Week Time sheet.

I believe I may have the Range and Criteria reversed. but at 1 AM I'm cross-eyed
here is the VBA

VBA Code:
Sub IndexMatchWithTwoCriteria()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim i As Long, j As Long
    Dim criteria1 As String, criteria2 As String
    Dim result As Variant
    Dim searchRange1 As Range, searchRange2 As Range, indexRange As Range

    ' Set your worksheets (adjust the sheet names as needed)
    Set ws1 = ThisWorkbook.Sheets("Payroll Week Time")
    Set ws2 = ThisWorkbook.Sheets("Sheet2")

    ' Define the ranges for criteria and index on Sheet2
    Set searchRange1 = ws2.Range("A:A") ' Criteria1 range (Column A)
    Set searchRange2 = ws2.Range("B:B") ' Criteria2 range (Column B)
    Set indexRange = ws2.Range("E:E") ' Index range (Column E)

    ' Loop through all rows in Sheet1 (B2:B90)
    For i = 2 To 90
        ' Get the criteria1 value from Sheet1
        criteria1 = ws1.Cells(i, "B").Value
       
        ' Loop through all columns in Sheet1 (F2:L2)
        For j = 6 To 12 ' Columns F to L
            ' Get the criteria2 value from Sheet1
            criteria2 = ws1.Cells(2, j).Value
           
            ' Perform the INDEX MATCH using WorksheetFunction.Match
            On Error Resume Next
            result = Application.WorksheetFunction.Index(indexRange, _
                        Application.WorksheetFunction.Match(1, _
                            (searchRange1 = criteria1) * (searchRange2 = criteria2), 0))
            On Error GoTo 0

            ' If a match is found, place the result in the corresponding cell in Sheet1
            If Not IsError(result) Then
                ws1.Cells(i, j).Value = result
            Else
                ws1.Cells(i, j).Value = "Not Found" ' Optional: to handle cases where no match is found
            End If
        Next j
    Next i

    MsgBox "Index Match operation completed.", vbInformation
End Sub

The code tags and all the other options are greyed out. sorry for the sloppy cut and paste on the code.

Also after I posted I realized that the week was wrong 34 vs 35 so the dates were off, I changed it and re-ran it and still no mas. thanks in advance for any help
 
Last edited by a moderator:
Great to hear that you found it easy to modify.
If you want to do a similar thing as Trim to replace ";" you could add this line.
VBA Code:
    arrSrc = Application.Substitute(arrSrc, ";", "")
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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