how to use vlookup function to fetch values from multiple sheets.

Aman Chalotra

New Member
Joined
Mar 9, 2017
Messages
14
Hi All !!!

I have a routine work which i wish to automate with the help of VBA macro. I have a sheet with say 8 employees. I want to fetch there monthly salary from other sheet. Previously I was having only one sheet to get salary value but now I have to get it form multiple sheets. Here I want to search employee salary wrt to Employee ID from two sheets June and April. And paste in sheet1 next to employee name in two columns.

I was easy to use vlookup macro recorder but with sheets increasing its not a good Idea to use macro recorder. I have tried hard but not able to get the string to start code.

Please provide a basic code to get values.

My main sheet looks like this i.e. sheet1 :
[TABLE="width: 256"]
<tbody>[TR]
[TD="class: xl69, width: 64"]ID[/TD]
[TD="class: xl69, width: 64"]Name[/TD]
[TD="class: xl67, width: 64"]June[/TD]
[TD="class: xl67, width: 64"]April[/TD]
[/TR]
[TR]
[TD="class: xl66"]D03[/TD]
[TD="class: xl67"]Jammes[/TD]
[TD="class: xl67"][/TD]
[TD="class: xl67"][/TD]
[/TR]
[TR]
[TD="class: xl67"]D04[/TD]
[TD="class: xl67"]Mathew[/TD]
[TD="class: xl67"][/TD]
[TD="class: xl67"][/TD]
[/TR]
[TR]
[TD="class: xl66"]D15[/TD]
[TD="class: xl67"]Anderson[/TD]
[TD="class: xl67"][/TD]
[TD="class: xl67"][/TD]
[/TR]
[TR]
[TD="class: xl67"]D20[/TD]
[TD="class: xl67"]Hayden[/TD]
[TD="class: xl67"][/TD]
[TD="class: xl67"][/TD]
[/TR]
[TR]
[TD="class: xl67"]D12[/TD]
[TD="class: xl67"]Stuart[/TD]
[TD="class: xl67"][/TD]
[TD="class: xl67"][/TD]
[/TR]
[TR]
[TD="class: xl67"]D09[/TD]
[TD="class: xl67"]Broad[/TD]
[TD="class: xl67"][/TD]
[TD="class: xl67"][/TD]
[/TR]
[TR]
[TD="class: xl68"]D30[/TD]
[TD="class: xl67"]Donald[/TD]
[TD="class: xl67"][/TD]
[TD="class: xl67"][/TD]
[/TR]
</tbody>[/TABLE]
Destination sheet One i.e. June is
[TABLE="width: 128"]
<tbody>[TR]
[TD="class: xl70, width: 64"]ID[/TD]
[TD="class: xl69, width: 64"]Salary[/TD]
[/TR]
[TR]
[TD="class: xl68"]D03[/TD]
[TD="class: xl69, align: right"]1000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D04[/TD]
[TD="class: xl69, align: right"]2000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D05[/TD]
[TD="class: xl69, align: right"]3000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D06[/TD]
[TD="class: xl69, align: right"]4000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D07[/TD]
[TD="class: xl69, align: right"]5000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D08[/TD]
[TD="class: xl69, align: right"]6000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D09[/TD]
[TD="class: xl69, align: right"]7000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D10[/TD]
[TD="class: xl69, align: right"]8000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D11[/TD]
[TD="class: xl69, align: right"]9000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D12[/TD]
[TD="class: xl69, align: right"]10000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D13[/TD]
[TD="class: xl69, align: right"]11000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D14[/TD]
[TD="class: xl69, align: right"]12000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D15[/TD]
[TD="class: xl69, align: right"]13000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D16[/TD]
[TD="class: xl69, align: right"]14000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D17[/TD]
[TD="class: xl69, align: right"]15000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D18[/TD]
[TD="class: xl69, align: right"]16000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D19[/TD]
[TD="class: xl69, align: right"]17000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D20[/TD]
[TD="class: xl69, align: right"]18000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D21[/TD]
[TD="class: xl69, align: right"]19000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D22[/TD]
[TD="class: xl69, align: right"]20000[/TD]
[/TR]
</tbody>[/TABLE]
And second destination sheet i.e. April is:
[TABLE="width: 128"]
<tbody>[TR]
[TD="class: xl70, width: 64"]ID[/TD]
[TD="class: xl69, width: 64"]Salary[/TD]
[/TR]
[TR]
[TD="class: xl68"]D03[/TD]
[TD="class: xl69, align: right"]100000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D04[/TD]
[TD="class: xl69, align: right"]150000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D05[/TD]
[TD="class: xl69, align: right"]200000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D06[/TD]
[TD="class: xl69, align: right"]250000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D07[/TD]
[TD="class: xl69, align: right"]300000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D08[/TD]
[TD="class: xl69, align: right"]350000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D09[/TD]
[TD="class: xl69, align: right"]400000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D10[/TD]
[TD="class: xl69, align: right"]450000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D11[/TD]
[TD="class: xl69, align: right"]500000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D12[/TD]
[TD="class: xl69, align: right"]550000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D13[/TD]
[TD="class: xl69, align: right"]600000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D14[/TD]
[TD="class: xl69, align: right"]650000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D15[/TD]
[TD="class: xl69, align: right"]700000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D16[/TD]
[TD="class: xl69, align: right"]750000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D17[/TD]
[TD="class: xl69, align: right"]800000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D18[/TD]
[TD="class: xl69, align: right"]850000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D19[/TD]
[TD="class: xl69, align: right"]900000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D20[/TD]
[TD="class: xl69, align: right"]950000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D21[/TD]
[TD="class: xl69, align: right"]1000000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D22[/TD]
[TD="class: xl69, align: right"]1050000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D23[/TD]
[TD="class: xl69, align: right"]1100000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D24[/TD]
[TD="class: xl69, align: right"]1150000[/TD]
[/TR]
[TR]
[TD="class: xl68"]D25[/TD]
[TD="class: xl69, align: right"]1200000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D26[/TD]
[TD="class: xl69, align: right"]1250000[/TD]
[/TR]
[TR]
[TD="class: xl71"]D27[/TD]
[TD="align: right"]1300000[/TD]
[/TR]
[TR]
[TD="class: xl69"]D28[/TD]
[TD="align: right"]1350000[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Aman Chalotra,

Here is a macro solution for you to consider.

Sample raw data worksheets:


Excel 2007
AB
1IDSalary
2D031000
3D042000
4D053000
5D064000
6D075000
7D086000
8D097000
9D108000
10D119000
11D1210000
12D1311000
13D1412000
14D1513000
15D1614000
16D1715000
17D1816000
18D1917000
19D2018000
20D2119000
21D2220000
22
June



Excel 2007
AB
1IDSalary
2D03100000
3D04150000
4D05200000
5D06250000
6D07300000
7D08350000
8D09400000
9D10450000
10D11500000
11D12550000
12D13600000
13D14650000
14D15700000
15D16750000
16D17800000
17D18850000
18D19900000
19D20950000
20D211000000
21D221050000
22D231100000
23D241150000
24D251200000
25D261250000
26D271300000
27D281350000
28
April



Excel 2007
ABCDE
1IDNameJuneApril
2D03Jammes
3D04Mathew
4D15Anderson
5D20Hayden
6D12Stuart
7D09Broad
8D30Donald
9
Sheet1


And, after the macro:


Excel 2007
ABCDE
1IDNameJuneApril
2D03Jammes1000100000
3D04Mathew2000150000
4D15Anderson13000700000
5D20Hayden18000950000
6D12Stuart10000550000
7D09Broad7000400000
8D30Donald
9
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub FindMonthlySalaryPerID()
' hiker95, 03/12/2017, ME995242
Dim r As Range, id As Range, lc As Long, c As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  For Each r In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    For c = 3 To lc Step 1
      Set id = Sheets(.Cells(1, c).Value).Columns(1).Find(r.Value, LookAt:=xlWhole)
      If Not id Is Nothing Then
        .Cells(r.Row, c).Value = Sheets(.Cells(1, c).Value).Cells(id.Row, 2).Value
      End If
    Next c
  Next r
  .UsedRange.Columns.AutoFit
End With
Application.ScreenUpdating = False
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the FindMonthlySalaryPerID macro.
 
Upvote 0
Hi,

Creat list with name ShtList containing name of all sheet and refer in below formula

=SUMPRODUCT(SUMIF(INDIRECT("'"&ShtList&"'!B2:B9"),B3,INDIRECT("'"&ShtList&"'!c2:c9")))
 
Upvote 0
Hi hiker95.
Above code is giving Subscriopt out of range error at below
lineSet id = Sheets(.Cells(1, c).Value).Columns(1).Find(r.Value, LookAt:=xlWhole)

What could be the problem please guide.
 
Upvote 0
Hi hiker95.
Above code is giving Subscriopt out of range error at below
lineSet id = Sheets(.Cells(1, c).Value).Columns(1).Find(r.Value, LookAt:=xlWhole)

What could be the problem please guide.

Aman Chalotra,

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?

3. Are the screenshots in my reply #2 correct?

4. Are their formulae in worksheets June, April, and, Sheet1?


If my screenshots are correct, then, can we see your actual raw data workbook/worksheets?

You can post your workbook/worksheets to the following free site (sensitive data changed), mark the workbook for sharing, and, provide us with a link to your workbook:

https://dropbox.com
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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