Excel VBA to Match Value in Range: 3 Methods

Method 1 – Match a Value in Range with VBA Match Function in Excel

STEPS:

  • Go to the Developer tab from the ribbon.
  • Click on Visual Basic to open the Visual Basic Editor. Alternatively, press Alt + 11 to open the Visual Basic Editor.

  • Another way to open Visual Basic Editor is just to right-click on the worksheet and click on View Code.

Match Value in Range with VBA Match Function in Excel

  • Insert the code there.

VBA Code:

Sub example1_match()
Range("G5").Value = WorksheetFunction.Match(Range("F5").Value, Range("D5:D10"), 0)
End Sub
  • Press the F5 key on your keyboard or click on the Run sub button.

Match Value in Range with VBA Match Function in Excel

  • You can see that the match is found in position 5.

How Does the VBA Code Work?

  • Sub example1_match(): This means we define a subprocedure by giving the macro name.
  • Range("G5").Value: We want the output to be stored in cell G5.
  • WorksheetFunction: By using this code we will be able to access the VBA functions.
  • Match(Range("F5").Value, Range("D5:D10"), 0): Here, we use the Match function in VBA. As we want to take the value from cell F5 and find out the position in range D5:D10.
  • End Sub: This means we end the procedure.

Method 2 – Use Excel VBA to Match Value from Another Worksheet

STEPS:

  • Right-click on the sheet name and select View Code.

  • Insert this VBA code.

VBA Code:

Sub example2_match()
Sheets("Result").Range("C5").Value = WorksheetFunction.Match(Sheets("Result").Range("C5").Value, Sheets("Data").Range("D5:D10"), 0)
End Sub
  • Run the code by pressing the F5 key or clicking the Run Sub button.

  • The result is found in the Result sheet.

Use Excel VBA to Match Value from Another Worksheet


Method 3 – Excel VBA Loops to Get Matched Value in Range

STEPS:

  • Right-click on the sheet name and select View Code.

  • Insert this code:

VBA Code:

Sub example3_match()
Dim i As Integer
For i = 5 To 8
Cells(i, 7).Value = WorksheetFunction.Match(Cells(i, 6).Value, Range("D5:D10"), 0)
Next i
End Sub
  • Pressing the F5 key or clicking the Run Sub button will run the code.

  • You will be able to see the result in Column G.

How Does the VBA Code Work?

  • For i = 5 To 8: This means we want the loop runs starting with row 5 and ending with row 8.
  • Cells(i, 7).Value: This saves the value of the resultant locations in each row from 5 to 8 rows in Column G which is column number 7.
  • Match(Cells(i, 6).Value, Range("D5:D10"), 0): Cells can be matched using the Match function (i, 6). Values search for each Lookup value found in rows 5 through 8 of the 6th column. Then searched in array D5:D10 on an Excel sheet where data is available.

Things to Keep in Mind

  • If the match type is missing or not specified, it is assumed to be 1.
  • If no match is detected, the field will be blank.
  • The lookup value can be a numeric, character, or logical data, or a cell reference to a quantity, text, or logical significance.

Download Practice Workbook

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Sabrina Ayon
Sabrina Ayon

Sabrina Ayon, a Computer Science and Engineering graduate from United International University, has been an integral part of the ExcelDemy project for two years. She authored 150+ articles, excelling in instructing through visually engaging Excel tutorials. With a passion for teaching, Sabrina conducted sessions on Excel VBA, sharing her knowledge and insights with others. Currently holding the position of Project Manager for the ExcelDemy Visual Development Project, she oversees various aspects of the project, ensuring its smooth operation... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo