vlookup excel code example

Example 1: excel vba vlookup

Sub vlookup1()
	Dim student_id As Long
	Dim marks As Long
	student_id = 11004
	Set myrange = Range(“B4:D8”)
	marks = Application.WorksheetFunction.VLookup(student_id, myrange, 3, False)
End Sub

Example 2: vba code to vlookup from another workbook

Sub test()
Application.ScreenUpdating = False

Dim sw As Workbook
Dim dw As Workbook
Dim srng As Range

swname$ = "BB_Finacle ID-Mar'19.xlsb"
swpath$ = ThisWorkbook.Path & "\" & swname
Set dw = ThisWorkbook
c& = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
Workbooks.Open (swpath)
Set sw = Workbooks(swname)
Set srng = sw.Sheets(1).Range("B:R")

    Sheet1.Range("B2:B" & c).Formula = "=VLookup(A2,[BB_Finacle ID-Mar'19.xlsb]SHEET1!" & srng.Address & ", 3, False)"
  
  
    Sheet1.Range("B2:B" & c).Copy
    Sheet1.Range("B2").PasteSpecial xlValues
    Application.CutCopyMode = False
sw.Close

Application.ScreenUpdating = True
End Sub

Tags:

Vb Example