Excel vba loop to find records matching multiple search criteria and multiple sheets

SoniaLady

New Member
Joined
Feb 17, 2022
Messages
8
Office Version
  1. 365
Hi @all,
I would like to implement the following function in VBA as a procedure.

=INDEX(Zones!$G$3:$S$273;MATCH($A3;Zones!$C$3:$C$272);MATCH($B3;Zones!$G$2:$S$2))

I have two sheets ("Data" and Zones"). I search in the sheet Zones for zone numbers, to be able to determine later also the prices. The search criteria are country and delivery service is in the sheet "Data A2:A" and delivery service in "Data B2:B"). The country in the data sheet "Zones" is in column A, the delivery service in rows G2:S2. The searched zones numbers are in G3:S272). I would appreciate it very much if someone could help here.

Many thanks in advance.
Regards
Sonia
 

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.
I have now created the following code myself, but I get the error message: "Unable to get tech Match property of the WorksheetFunction class". Can anyone help to fix that?

VBA Code:
Public Sub GetZones()
    Dim wSSource As Worksheet: Set wSSource = Worksheets("Zones")
    Dim wSTarget As Worksheet: Set wSTarget = Worksheets("Data")
    Dim rngMatrix As Range: Set rngMatrix = wSSource.Range("C3:S272")
    Dim rngRow As Range: Set rngRow = wSSource.Range("G2:S2")
    Dim rngColumn As Range: Set rngColumn = wSSource.Range("A3:A272")
    Dim rngTarget As Range: Set rngTarget = wSTarget.Range("C2")
   
    Dim strName As String: strName = Worksheets("Data").Cells(1, 1)
    Dim Zones As String: Zones = Worksheets("Data").Cells(2, 1)
   
        rngTarget.Value = WorksheetFunction.Index(rngMatrix, _
                            WorksheetFunction.Match(strName, rngColumn, 0), WorksheetFunction.Match(Zones, rngRow, 0))
                           
End Sub
 
Upvote 0
Hi
Replace
VBA Code:
 rngTarget.Value = WorksheetFunction.Index(rngMatrix, _
                            WorksheetFunction.Match(strName, rngColumn, 0), WorksheetFunction.Match(Zones, rngRow, 0))

With

VBA Code:
With  rngTarget
.Formula ="= WorksheetFunction.Index(rngMatrix, _
                            WorksheetFunction.Match(strName, rngColumn, 0), WorksheetFunction.Match(Zones, rngRow, 0))"
.value=.value
End with

Not tested
 
Upvote 0
Thanks mohadin. Now the code goes through, but as result I get #NAME?. What is wrong now?
 
Upvote 0
Try this way
Updated
VBA Code:
Sub test()
    With rngTarget
        .Value = "=WorksheetFunction.Index(rngMatrix, WorksheetFunction.Match(strName, rngColumn, 0), WorksheetFunction.Match(Zones, rngRow, 0))"
        .Value = .Value
    End With
End Sub
 
Last edited:
Upvote 0
VBA Code:
Sub test()
    With rngTarget
        .Value = "=WorksheetFunction.Index(rngMatrix, WorksheetFunction.Match(strName, rngColumn, 0), WorksheetFunction.Match(Zones, rngRow, 0))"
       ' .Value = .Value
    End With
End Sub

You should have a formula in C2
 
Last edited by a moderator:
Upvote 0
Use Application instead of WorksheetFunction
VBA Code:
Public Sub GetZones()
    Dim wSSource As Worksheet: Set wSSource = Worksheets("Zones")
    Dim wSTarget As Worksheet: Set wSTarget = Worksheets("Data")
    Dim rngMatrix As Range: Set rngMatrix = wSSource.Range("C3:S272")
    Dim rngRow As Range: Set rngRow = wSSource.Range("C2:S2")
    Dim rngColumn As Range: Set rngColumn = wSSource.Range("A3:A272")
    Dim rngTarget As Range: Set rngTarget = wSTarget.Range("C2")
  
    Dim strName As String: strName = Worksheets("Data").Cells(1, 1)
    Dim Zones As String: Zones = Worksheets("Data").Cells(2, 1)
    'If Not IsError(Application.Match(strName, rngColumn, 0)) And Not IsError(Application.Match(Zones, rngRow, 0)) Then
  
        rngTarget.Value = Application.Index(rngMatrix, _
                            Application.Match(strName, rngColumn, 0), Application.Match(Zones, rngRow, 0))
   ' End If
End Sub
You may need to add "If Not IsError...", to avoid error.
 
Upvote 0
Use Application instead of WorksheetFunction
VBA Code:
Public Sub GetZones()
    Dim wSSource As Worksheet: Set wSSource = Worksheets("Zones")
    Dim wSTarget As Worksheet: Set wSTarget = Worksheets("Data")
    Dim rngMatrix As Range: Set rngMatrix = wSSource.Range("C3:S272")
    Dim rngRow As Range: Set rngRow = wSSource.Range("C2:S2")
    Dim rngColumn As Range: Set rngColumn = wSSource.Range("A3:A272")
    Dim rngTarget As Range: Set rngTarget = wSTarget.Range("C2")
 
    Dim strName As String: strName = Worksheets("Data").Cells(1, 1)
    Dim Zones As String: Zones = Worksheets("Data").Cells(2, 1)
    'If Not IsError(Application.Match(strName, rngColumn, 0)) And Not IsError(Application.Match(Zones, rngRow, 0)) Then
 
        rngTarget.Value = Application.Index(rngMatrix, _
                            Application.Match(strName, rngColumn, 0), Application.Match(Zones, rngRow, 0))
   ' End If
End Sub
You may need to add "If Not IsError...", to avoid error.
bebo02199 thank you very much for your hint and the code. I tried your code but unfortunately no value is returned. Here how my data sheet looks like.The zone number/value should appear in C2. The zone number for Brasil should be 10.
Data.png
Zones.png
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,245
Members
448,952
Latest member
kjurney

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