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 :
IDNameJuneApril
D03Jammes
D04Mathew
D15Anderson
D20Hayden
D12Stuart
D09Broad
D30Donald

<tbody>
</tbody>
Destination sheet One i.e. June is
IDSalary
D031000
D042000
D053000
D064000
D075000
D086000
D097000
D108000
D119000
D1210000
D1311000
D1412000
D1513000
D1614000
D1715000
D1816000
D1917000
D2018000
D2119000
D2220000

<tbody>
</tbody>
And second destination sheet i.e. April is:
IDSalary
D03100000
D04150000
D05200000
D06250000
D07300000
D08350000
D09400000
D10450000
D11500000
D12550000
D13600000
D14650000
D15700000
D16750000
D17800000
D18850000
D19900000
D20950000
D211000000
D221050000
D231100000
D241150000
D251200000
D261250000
D271300000
D281350000

<tbody>
</tbody>
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
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,214,657
Messages
6,120,764
Members
448,991
Latest member
Hanakoro

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