Vba vlookup

2avrham

Board Regular
Joined
May 12, 2014
Messages
104
Office Version
  1. 365
Hi,

I’m using the next VLOOKUP function at excel and itworks fine à =VLOOKUP($B2,SHEET20!$1:$1048576,1,0) (im running it from different sheet (sheet1).

My problem is that when I’m record Macro with VLOOKUP formula it’s notworking.

I understood that I need to make some changes to fit VLOOKUP functionto “VBA” VLOOKUP but I failed to do it.

Any idea how can I edit this VLOOKUP to “VBA VLOOKUP”?


My code with my last try:


  • I want torun VLOOKUP function on cell M2 at sheet1

    Sub aaa()



ActiveSheet.ListObjects("MyData").Range.AutoFilter Field:=2,Criteria1:= _
"<>"
Columns("A:B").Select
Selection.Copy
Sheets.AddAfter:=ActiveSheet
ActiveSheet.Paste

StartDate =DateAdd("d", "-7", Date)
EndDate = Now

Range("A1:b1").AutoFilter Field:=2, _
Criteria1:=">=" & StartDate, _
Operator:=xlAnd, _
Criteria2:="<=" & EndDate


Columns("A:B").Select
Selection.Copy
Range("C1").Select
ActiveSheet.Paste
Columns("A:B").Select
Application.CutCopyMode =False
Selection.DeleteShift:=xlToLeft
Sheets("Sheet1").Select
ActiveWorkbook.SlicerCaches("Slicer_Time_filter").ClearManualFilter
ActiveWindow.ScrollColumn =2
ActiveWindow.ScrollColumn =3
ActiveWindow.ScrollColumn =4
ActiveWindow.ScrollColumn =5
ActiveWindow.ScrollColumn =6



Range("M1").Select
ActiveCell.FormulaR1C1 ="Plat"
Range("M2").Select

Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Sheet1")

With ws
.Range("M2").Formula ="=VLOOKUP(B2,Sheet21!$A:$B,1,0)"
End With



Range("M2").Select
Selection.FillDown


End Sub

Thanks!!!

 
Are you trying to lookup the value from col B on sheet1, in col A on sheet21 & if found return the value from col B on sheet21?
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
The Formula you provided is returning the same value that you are looking up, so I have made the same assumption as offthelip, in that you want to return the col B value.
Code:
Sub MyVlookup()
   Dim cl As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Sheet21")
      For Each cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
         Dic(cl.Value) = cl.Offset(, 1).Value
      Next cl
   End With
   With Sheets("sheet1")
      For Each cl In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
         If Dic.exists(cl.Value) Then cl.Offset(, 11).Value = Dic(cl.Value)
      Next cl
   End With
End Sub

I want to return Sheet 21 A column results ( now im getting column B)
 
Upvote 0
So you want to return the same value that you looked up?
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
with my code all you need to do to pick up column A is change this line:
Code:
outarr(i, 1) = inarr(j, 2)
to
Code:
outarr(i, 1) = inarr(j, 1)
However I do wonder why you were trying to do this with Vllookup in the first place when it appears that all you were trying to do was check to see if one variable existed in another list. For information you can do this quite easily with the MATCH function or the COUNTIF function.
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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