VLOOKUP using input boxes

Aranoless

New Member
Joined
Aug 29, 2020
Messages
1
Office Version
  1. 2010
Platform
  1. Windows
Hello, I'm very much a beginner in VBA coding. What I wanted to do was to make a macro to vlookup a certain column in table1 to be pasted to table2, and to use input boxes so users can specify which column they want to vlookup.

Problem is, the code can't seem to recognize that I want a range of cells from the user and not just one cell for the reference. The table_lookup value also only took in one cell even after selecting a range of cells in the input boxes. I would appreciate any help regarding this.

This is my code.

VBA Code:
Sub VLOOKUPtry()
   
    Dim reff As String
    Dim lookuptable As Range
    Dim columnin As Integer
    Dim exactma As Boolean

    Set lookuptable = Application.Selection

    reff = Application.InputBox("What is your reference?")
    lookuptable = Application.InputBox("What is the range of the data?")
    columnin = Application.InputBox("What number is the column in from the left?")
    exactma = Application.InputBox("If exact match, press 0")

    ActiveCell.FormulaR1C1 = "=VLOOKUP(""" & reff & """, """ & lookuptable.Address & """, """ & columin & """, """ & exactma & """)"

End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
The way that you have written it, lookup table is being set to the cell that was selected before you started to run the code, not the range set by the inputbox.
This should fix that part, but there are a couple of other things that you might need to change as well.

In the last line you have used FormulaR1C1, that probably just needs to be Formula.
In vba, a Boolean is false by default so if the user ignores the prompt to press 0 then it will still be exact match.
VBA Code:
Sub VLOOKUPtry()
   
    Dim reff As String
    Dim lookuptable As Range
    Dim columnin As Integer
    Dim exactma As Boolean

    reff = Application.InputBox("What is your reference?")
    Set lookuptable = Application.InputBox(Prompt:="What is the range of the data?", Type:=8)
    columnin = Application.InputBox("What number is the column in from the left?")
    exactma = Application.InputBox("If exact match, press 0")

    ActiveCell.FormulaR1C1 = "=VLOOKUP(""" & reff & """, """ & lookuptable.Address & """, """ & columin & """, """ & exactma & """)"

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,085
Members
449,206
Latest member
ralemanygarcia

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