VBA code for VLOOKUP - automated for an entire column

newbie111

New Member
Joined
Jun 2, 2020
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Hi, I wonder if anyone can help. Apologies I'm very new to this but here is what I'm trying to do...

I have a database (in sheet 1, columns A and B covering Name and Department).

Ideally, when a user adds a name into column A on sheet2 I would like it to search my database and return the correct department in column B. Sheet 2 is updated/completed on a daily basis so there is no defined range. The database will also be added to.

I have managed to get exactly what I need when I run it so far with this solely for one cell:

set Datatable = sheet1.range("A:B")
set Name = sheet2.range("A2")
Set Mark = sheet2.range("B2")

Mark.value = application.worksheetfunction.vlookup(Name, Datatable, 2, False)

I'm struggling with how to translate this into an automatic response when the user adds information into any row in column A....Any help is greatly appreciated!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Welcome to the MrExcel board!

Do you really need vba? What if you make a formal table on each worksheet & use a formula as I have done below. The formula will automatically extend to new rows in the sheet2 table and will reference all old and new rows in the sheet1 table.

Here is my Sheet1. I have named this table 'DBtable'. Once the table has been created (Insert ribbon tab -> Table -> my table has headers) its name can be edited in the Name Manager on the Formulas ribbon tab.

newbie111 2020-06-02 1.xlsm
AB
1NameDepartment
2Name 1Dept 1
3Name 2Dept 2
4Name 3Dept 3
5Name 4Dept 1
6Name 5Dept 2
7Name 6Dept 3
8Name 7Dept 1
9
Sheet1



Then my Sheet2 table with formula

newbie111 2020-06-02 1.xlsm
AB
1NameDepartment
2Name 4Dept 1
3 
4Name 10Not found
5Name 6Dept 3
6
Sheet2
Cell Formulas
RangeFormula
B2:B5B2=IF([@Name]="","",IFERROR(VLOOKUP([@Name],DBtable,2,0),"Not found"))
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,554
Members
448,970
Latest member
kennimack

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