VBA with Lookups

dan8825

New Member
Joined
Dec 24, 2018
Messages
26
Hello,

I have found a thread on lookups with VBA but I cannot figure how to manipulate the lookup to match my needs. Can someone break down how a lookup translates into VBA?

Below is the code, which should open two sheets then perform a lookup. If I wanted to use the below lookup how do i use this in VBA?

=VLOOKUP(A2,'[Flat And Drop Report.xlsx]Sheet1'!$A:$C,2,0)

Thanks

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sub VlookMultipleWorkbooks()

Dim lookFor As Range
Dim srchRange As Range

Dim book1 As Workbook
Dim book2 As Workbook

Dim book2Name As String
book2Name
= "test.xls" 'modify it as per your requirement

Dim book2NamePath As String
book2NamePath
= ThisWorkbook.Path & "" & book2Name

Set book1 = ThisWorkbook

If IsOpen(book2Name) = False Then Workbooks.Open (book2NamePath)
Set book2 = Workbooks(book2Name)


Set lookFor = book1.Sheets(1).Cells(2, 1) ' value to find
Set srchRange = book2.Sheets(1).Range("B:C") 'source

lookFor
.Offset(0, 1).Value = Application.VLookup(lookFor, srchRange, 2, False)

End Sub

Function IsOpen(strWkbNm As String) As Boolean

On Error Resume Next

Dim wBook As Workbook
Set wBook = Workbooks(strWkbNm)

If wBook Is Nothing Then 'Not open
IsOpen
= False
Set wBook = Nothing
On Error GoTo 0
Else
IsOpen
= True
Set wBook = Nothing
On Error GoTo 0
End If

End Function</code>
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Does this work? My VBA is very rusty

Code:
Sub FndVal()
Lastrow = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
var1=Worksheets("Sheet1").Cells(2,1)
j=0
i=0
While (i<>Lastrow) and (j=0)
i=i+1
If Worksheets("Sheet1").cells(i,1)=var1 then
    j=1:k=Worksheets("Sheet1").cells(i,3)
End If
Wend
If j=0 then MsgBox "Not found"
Else MsgBox k
End Sub

var1 is the lookup value (A2)
i is the loop that runs from row 1 to the last row in column A
j is a flag that gets set if A2 is found (I dont like breaking out of loops)
k is the value found in column C
 
Last edited:
Upvote 0
Hello,
Below is the code, which should open two sheets then perform a lookup. If I wanted to use the below lookup how do i use this in VBA?
=VLOOKUP(A2,'[Flat And Drop Report.xlsx]Sheet1'!$A:$C,2,0)

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">
</code>
Code:
[COLOR=#101094][FONT=Consolas][FONT=inherit]
Set[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] lookFor [/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]=[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] book1[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit].[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Sheets[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]([/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]1[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]).[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Cells[/FONT][/FONT][/COLOR][FONT=Consolas][FONT=inherit][COLOR=#303336]([/COLOR][COLOR=#7d2727]2[/COLOR][/FONT][/FONT][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]1[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit])[/FONT][/FONT][/COLOR][COLOR=#858C93][FONT=Consolas][FONT=inherit]' THIS IS A2
[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]
[/FONT][/FONT][/COLOR][COLOR=#101094][FONT=Consolas][FONT=inherit]Set[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] srchRange [/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]=[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] book2[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit].[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Sheets[/FONT][/FONT][/COLOR][FONT=Consolas][FONT=inherit][COLOR=#303336]([/COLOR][COLOR=#7d2727]"Sheet1"[/COLOR][/FONT][/FONT][COLOR=#303336][FONT=Consolas][FONT=inherit]).[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Range[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]([/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]"A:C"[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit])  [/FONT][/FONT][/COLOR][COLOR=#858C93][FONT=Consolas][FONT=inherit]' book2 --> Flat And Drop Report.xlsx (if exists) + Sheet1!A:C as requested above[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]

[B]FoundValue[/B] =[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] Application[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit].[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]VLookup[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]([/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]lookFor[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] srchRange[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]2[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]False[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit])     ' should works


[/FONT][/FONT][/COLOR]

 
Upvote 0
Code:
[COLOR=#101094][FONT=Consolas][FONT=inherit]
Set[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] lookFor [/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]=[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] book1[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit].[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Sheets[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]([/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]1[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]).[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Cells[/FONT][/FONT][/COLOR][FONT=Consolas][FONT=inherit][COLOR=#303336]([/COLOR][COLOR=#7d2727]2[/COLOR][/FONT][/FONT][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]1[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit])[/FONT][/FONT][/COLOR][COLOR=#858C93][FONT=Consolas][FONT=inherit]' THIS IS A2
[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]
[/FONT][/FONT][/COLOR][COLOR=#101094][FONT=Consolas][FONT=inherit]Set[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] srchRange [/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]=[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] book2[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit].[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Sheets[/FONT][/FONT][/COLOR][FONT=Consolas][FONT=inherit][COLOR=#303336]([/COLOR][COLOR=#7d2727]"Sheet1"[/COLOR][/FONT][/FONT][COLOR=#303336][FONT=Consolas][FONT=inherit]).[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Range[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]([/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]"A:C"[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit])  [/FONT][/FONT][/COLOR][COLOR=#858C93][FONT=Consolas][FONT=inherit]' book2 --> Flat And Drop Report.xlsx (if exists) + Sheet1!A:C as requested above[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]

[B]FoundValue[/B] =[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] Application[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit].[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]VLookup[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]([/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]lookFor[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] srchRange[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]2[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]False[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit])     ' should works


[/FONT][/FONT][/COLOR]



Thank you both! will get testing, with this code, what part of it dictates its A2? If I wanted it to be B3 for example how would this write?
 
Upvote 0
Thanks that makes perfect sense.

How do I tell which cell the lookup to go into? so I want to find Column A but I want this to return in column B for example?
 
Upvote 0
Code:
[COLOR=#101094][FONT=Consolas][FONT=inherit]
Set[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] lookFor [/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]=[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] book1[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit].[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Sheets[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]([/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]1[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]).[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Cells[/FONT][/FONT][/COLOR][FONT=Consolas][FONT=inherit][COLOR=#303336]([/COLOR][COLOR=#7d2727]2[/COLOR][/FONT][/FONT][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]1[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit])[/FONT][/FONT][/COLOR][COLOR=#858C93][FONT=Consolas][FONT=inherit]' THIS IS A2
[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]
[/FONT][/FONT][/COLOR][COLOR=#101094][FONT=Consolas][FONT=inherit]Set[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] srchRange [/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]=[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] book2[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit].[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Sheets[/FONT][/FONT][/COLOR][FONT=Consolas][FONT=inherit][COLOR=#303336]([/COLOR][COLOR=#7d2727]"Sheet1"[/COLOR][/FONT][/FONT][COLOR=#303336][FONT=Consolas][FONT=inherit]).[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]Range[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]([/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]"A:C"[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit])  [/FONT][/FONT][/COLOR][COLOR=#858C93][FONT=Consolas][FONT=inherit]' book2 --> Flat And Drop Report.xlsx (if exists) + Sheet1!A:C as requested above[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]

[B]FoundValue[/B] =[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] Application[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit].[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]VLookup[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]([/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit]lookFor[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit] srchRange[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]2[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit],[/FONT][/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas][FONT=inherit]False[/FONT][/FONT][/COLOR][COLOR=#303336][FONT=Consolas][FONT=inherit])     ' should works


[/FONT][/FONT][/COLOR]



How do I tell which cell the lookup to go into? so I want to find Column A but I want this to return in column B for example?
 
Upvote 0
So you want to put the VLOOKUP function in a particular?
One little tool/trick you can use is the Macro Recorder.
If you turn on the Macro Recorder, and record yourself manually entering the VLOOKUP formula in the cell you want it to go into, and then stop the Macro Recorder and look at your code, it will show you what that VBA code looks like.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,213
Members
453,024
Latest member
Wingit77

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
Back
Top