简体   繁体   中英

VLOOKUP in VBA Excel VLOOKUP

I am trying to create a VLOOKUP macro that prompts the user to select cells and ranges.

Basically I want to look up a cell value in one tab against a range in another tab and return a column the user selects.

So...This is what I want...

VLOOKUP(""USER SELECT LOOKUP value", "USER SELECT TABLE ARRAY", "USER SELECT COL INDEX NUM", FALSE)

I have this so far but nothing returns when the macro finishes running...

Subject = Application.Input box("select Subject to be looked up")
Sheet = Application.Input box("select tab/range")

          Formula = "=VLOOKUP(""Subject"",""Sheet"",3,False)"

end sub

You need to pull the variables out of the quotes and concatenate using & .

Also you want to make them ranges and use their address in the formula:

Dim subject As Range
Dim sheet As Range
Dim clm as long
Set subject = Application.InputBox(prompt:="select Subject to be looked up", Type:=8)
Set sheet = Application.InputBox(prompt:="select tab/range", Type:=8)
clm = Application.InputBox(prompt:="Relative Column Reference", Type:=1)
'Change the range to the cell into which you want the formula placed.
ActiveSheet.Range("A1").Formula = "=VLOOKUP(" & Subject.Address(0,0) & "," & sheet.Address(0, 0, xlA1, 1) & "," & clm & ",False)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM