VLOOKUP Function in VBA
We will show you how to use it in VBA through examples VLOOKUP
.
VLOOKUP()
Using functions
in VBA
In Excel, we can use VLOOKUP()
the function to find a value in an array and return its comparable value from another column. The value we want to look for should exist in the first column.
We also have to indicate our requirements, whether we want a perfect match or the closest match to the value. We can use the worksheet access VLOOKUP
.
grammar:
# VBA
VLOOKUP(search_value,range,returnIndex_num,[lookupRange])
search_value
represents the value we are searching for. range
is the range we are working with, returnIndex_num
is the column number we want the value returned, lookupRange
and indicates whether the match is perfect or close.
lookupRange
There are two possibilities, true
either false
or 0
.1
Code:
#VBA
Application.WorksheetFunction.vlookup(search_value,range,returnIndex_num,[lookupRange])
ID
Let's take an example and try to find some data from it. Our data contains , , 姓名
and
for freelancers 提交的项目
.
If we want to search for ID 4
projects completed by freelancers with , we have to vlookup()
find them using the function in VBA.
First, we open the VBA Editor by pressing the ALT+ key and then create a new module from . Next, we create a new sub and assign variables to and .F11Insert > Module
lookForProj()
freelancer_id
projects completed
We will also set the scope in which we want to find freelancers.
Code:
#VBA
Sub lookForProj()
Dim freelancer_id As Long
Dim projects As Long
freelancer_id = 4
Set newRange = Range("A2:C6")
projects = Application.WorksheetFunction.VLookup(freelancer_id, newRange, 3, False)
MsgBox "Freelancer with ID: " & freelancer_id & " completed " & projects & " projects"
End Sub
Output:
Let's take another example. Consider data containing product names and their sales, and suppose we have added this data to columns B
and C
.
We want vlookup()
to search for the sales quantity of a product by using the function. First, let's Insert > Module
create a new module from and create a new sub lookForSales()
.
We'll assign variables product_id
to and sales
and set the scope in which we want to find freelancers.
Code:
# VBA
Sub lookForSales()
Dim product_id As Long
Dim sales As Long
product_id = 2
Set newRange = Range("A2:C6")
sales = Application.WorksheetFunction.VLookup(product_id, newRange, 3, False)
MsgBox "Product with ID: " & product_id & " sold " & sales & " times"
End Sub
Output:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Using functions in VBA
Publish Date:2025/04/17 Views:84 Category:Vba
-
This article will demonstrate how to create and use functions when working with VBA. Functions are used to return results that a subroutine cannot return. As programs grow longer and more complex, functions are a useful tool that makes it e
Remove duplicates in VBA
Publish Date:2025/04/16 Views:105 Category:Vba
-
We will explain how to remove duplicates in VBA by example. Remove duplicates in VBA When working with an Excel worksheet that contains a lot of data, it is likely that this data contains some duplicates. Any duplicates must be removed to a
Rounding in VBA
Publish Date:2025/04/16 Views:163 Category:Vba
-
Round() We will introduce the and functions in VBA through examples RoundUp() . Round() Using the or RoundUp() function in VBA When working with Excel worksheets containing numbers and calculations, we get results that are fractions. Someti
Adding a new row in VBA
Publish Date:2025/04/16 Views:195 Category:Vba
-
We will show you how to continue the code on the next line in VBA by example. We will also show you how to go to the next line in a message box using different methods in VBA. Adding a new row in VBA There are two different situations in pr
Sum function in VBA
Publish Date:2025/04/16 Views:126 Category:Vba
-
We will show you how to use sum in VBA. Sum function in VBA Sum is the most commonly used function in excel. This function is very useful as we can use sum to get the total from the financial table. We will learn how to use the sum function
Exit Sub in VBA
Publish Date:2025/04/16 Views:192 Category:Vba
-
We will look at different ways to exit a sub in VBA through examples. Using the Exit Sub Statement in VBA While using Sub in VBA, we may want to exit it or prevent it from executing further if an error occurs or the user provides wrong inpu
Calling a Sub in VBA
Publish Date:2025/04/16 Views:191 Category:Vba
-
We will show you how to call a sub within another sub through examples in VBA. Calling a Sub in VBA When dealing with multiple subroutines, we may encounter situations where we need to call multiple subroutines for the same function. Some f
Check if a string contains a substring in VBA
Publish Date:2025/04/16 Views:189 Category:Vba
-
This article demonstrates the use of Instr() the , , InstrRev() and Like functions to check whether a main string contains a substring. Use Instr() the function to check if the main string contains a substring Instr() Function syntax: InStr
Convert a string to a number in VBA
Publish Date:2025/04/16 Views:189 Category:Vba
-
This article will discuss how to convert a string to a number in VBA Excel. There is a step-by-step guide and many examples for us to understand. Convert a string to a number in VBA In VBA code, numbers saved as text must be converted to re