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
 

SoniaLady

New Member
Joined
Feb 17, 2022
Messages
8
Office Version
  1. 365
Sub test()
VBA Code:
    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
mohadin. The I get the following result:
result.png
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

SoniaLady

New Member
Joined
Feb 17, 2022
Messages
8
Office Version
  1. 365
The following code works well:

Excel Formula:
Sub GetZonesSec()
    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:S273")
    Dim rngRow As Range: Set rngRow = wSSource.Range("C2:S2")
    Dim rngColumn As Range: Set rngColumn = wSSource.Range("C3:C272")
    Dim rngTarget As Range: Set rngTarget = wSTarget.Range("C2")
    
    
    Dim strName As String: strName = Worksheets("Data").Range("A2")
    Dim Zones As String: Zones = Worksheets("Data").Range("B2")
    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

Maybe it can still be useful to someone.
 
Upvote 0
Solution

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,660
Office Version
  1. 2013
Platform
  1. Windows
With rngTarget
.Formula = "=Index(rngMatrix, .Match(strName, rngColumn, 0), Match(Zones, rngRow, 0))"
.Value = .Value
End With
 
Upvote 0

Forum statistics

Threads
1,187,026
Messages
5,961,168
Members
438,520
Latest member
annamemma

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
Top