A VBA Lookup
Readable:
Sub VBALookupFind()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet2")
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, "Q").End(xlUp).Row
If slRow < 2 Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(2, "Q"), sws.Cells(slRow, "Q"))
Dim slCell As Range
Set slCell = slrg.Find("text", slrg.Cells(slrg.Cells.Count), _
xlFormulas, xlWhole)
If slCell Is Nothing Then Exit Sub ' no match
Dim svCell As Range: Set svCell = slCell.EntireRow.Columns("R")
Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet1")
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, "B").End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, "B")
dCell.Value = svCell.Value
End Sub
Sub VBALookupMatch()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet2")
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, "Q").End(xlUp).Row
If slRow < 2 Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(2, "Q"), sws.Cells(slRow, "Q"))
Dim rIndex As Variant: rIndex = Application.Match("text", slrg, 0)
If IsError(rIndex) Then Exit Sub ' no match
Dim svrg As Range: Set svrg = slrg.EntireRow.Columns("R")
Dim svCell As Range: Set svCell = svrg.Cells(rIndex)
Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet1")
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, "B").End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, "B")
dCell.Value = svCell.Value
End Sub
Sub VBALookupFormula()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet2")
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, "Q").End(xlUp).Row
If slRow < 2 Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(2, "Q"), sws.Cells(slRow, "Q"))
Dim svrg As Range: Set svrg = slrg.EntireRow.Columns("R")
Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet1")
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, "B").End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, "B")
Dim dFormula As String
dFormula = "=IFERROR(INDEX('" & "Sheet2" _
& "'!" & svrg.Address(, 0) _
& "," & "MATCH(""" & "text" _
& """,'" & "Sheet2" _
& "'!" & slrg.Address(, 0) _
& ",0)),"""")"
dCell.Formula = dFormula
End Sub
Maintainable, Using Constants (Study With Debug.Print
)
Sub VBALookupFindConstants()
Const sName As String = "Sheet2" ' Source Worksheet Name
Const sfRow As Long = 2 ' Source First Row
Const slCol As String = "Q" ' Source Lookup Column
Const slValue As String = "text" ' Source Lookup Value
Const svCol As String = "R" ' Source Value Column
Const dName As String = "Sheet1" ' Destination Worksheet Name
Const dCol As String = "B" ' Destination Column
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, slCol).End(xlUp).Row
If slRow < sfRow Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(sfRow, slCol), sws.Cells(slRow, slCol))
Debug.Print "Source Lookup Range Address " & slrg.Address(, 0)
Dim slCell As Range
Set slCell = slrg.Find(slValue, slrg.Cells(slrg.Cells.Count), _
xlFormulas, xlWhole)
If slCell Is Nothing Then Exit Sub ' no match
Debug.Print "Source Lookup Cell Address " & slCell.Address(0, 0)
Dim svCell As Range: Set svCell = slCell.EntireRow.Columns(svCol)
Debug.Print "Source Value Cell Address " & svCell.Address(0, 0)
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, dCol).End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, dCol)
Debug.Print "Destination Cell Address " & dCell.Address(0, 0)
' Value...
dCell.Value = svCell.Value
Debug.Print "Destination Cell Value " & dCell.Value
End Sub
Sub VBALookupMatchConstants()
Const sName As String = "Sheet2" ' Source Worksheet Name
Const sfRow As Long = 2 ' Source First Row
Const slCol As String = "Q" ' Source Lookup Column
Const slValue As String = "text" ' Source Lookup Value
Const svCol As String = "R" ' Source Value Column
Const dName As String = "Sheet1" ' Destination Worksheet Name
Const dCol As String = "B" ' Destination Column
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, slCol).End(xlUp).Row
If slRow < sfRow Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(sfRow, slCol), sws.Cells(slRow, slCol))
Debug.Print "Source Lookup Range Address " & slrg.Address(, 0)
Dim rIndex As Variant: rIndex = Application.Match(slValue, slrg, 0)
If IsError(rIndex) Then Exit Sub ' no match
Debug.Print "Match Index " & rIndex
Dim svrg As Range: Set svrg = slrg.EntireRow.Columns(svCol)
Debug.Print "Source Value Range Address " & svrg.Address(, 0)
Dim svCell As Range: Set svCell = svrg.Cells(rIndex)
Debug.Print "Source Value Cell Address " & svCell.Address(0, 0)
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, dCol).End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, dCol)
Debug.Print "Destination Cell Address " & dCell.Address(0, 0)
' Value...
dCell.Value = svCell.Value
Debug.Print "Destination Cell Value " & dCell.Value
End Sub
Sub VBALookupFormulaConstants()
Const sName As String = "Sheet2" ' Source Worksheet Name
Const sfRow As Long = 2 ' Source First Row
Const slCol As String = "Q" ' Source Lookup Column
Const slValue As String = "text" ' Source Lookup Value
Const svCol As String = "R" ' Source Value Column
Const dName As String = "Sheet1" ' Destination Worksheet Name
Const dCol As String = "B" ' Destination Column
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, slCol).End(xlUp).Row
If slRow < sfRow Then Exit Sub ' no data
Dim slrg As Range
Set slrg = sws.Range(sws.Cells(sfRow, slCol), sws.Cells(slRow, slCol))
Debug.Print "Source Lookup Range Address " & slrg.Address(, 0)
Dim svrg As Range: Set svrg = slrg.EntireRow.Columns(svCol)
Debug.Print "Source Value Range Address " & svrg.Address(, 0)
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, dCol).End(xlUp).Row + 1
Dim dCell As Range: Set dCell = dws.Cells(dRow, dCol)
Debug.Print "Destination Cell Address " & dCell.Address(0, 0)
Dim dFormula As String
dFormula = "=IFERROR(INDEX('" & sName _
& "'!" & svrg.Address(, 0) _
& "," & "MATCH(""" & slValue _
& """,'" & sName _
& "'!" & slrg.Address(, 0) _
& ",0)),"""")"
Debug.Print "Destination Cell Formula " & dFormula
dCell.Formula = dFormula
Debug.Print "Destination Cell Value " & dCell.Value
End Sub