VB range lookup to return a value?!?

Dmo9997

New Member
Joined
Aug 19, 2015
Messages
3
Hi All -

I'm desperately hoping someone has the solution for what I'm trying to do here. Posting on mobile, so apologies in advance for not having code to paste outright.

I have two worksheets in my workbook, one titled teams and the other data. On sheet 'teams' I have a listing of agents and supervisors. Sups are listed starting at B1 through Y1, with their respective agents populating cells B2:Y32. Any agent in column B reports to the Sup in B1, etc.

Sheet 'data' contains something like 89000 rows of data about each individual...each agent name populating rows A2:A89000.

I need code to run a macro that looks at cell A2, looks that value (name) up in the array on sheet 'teams', and based on the column the value is found in, returns the value of that column, row 1 to sheet 'data', P2. Then I need the script to do that for every row on sheet 'data'. I have 25 Sups. 400 agents. And teams change monthly. So doing it manually or with a find/replace in the sheet itself MIGHT cause me to jump from a very high place.

I pray someone out there has the solution for this...I've tried every combination of if/then, v and h lookups, find/match, etc...I'm stuck.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
teamsABC
1John
John
Mary
2Mary
agent1
agent5
3Sue
agent2
agent6
4Tom
agent3
agent7
5Jane
agent4
agent8

<tbody>
</tbody>

This would be the teams table....names in bold are my Supervisors, agents are the team members


ABCD
1NameHours StaffedClaims HandledSupervisor Name
2agent1Data Point1Number1
3agent1Data Point2Number2
4agent3Data Point3Number3
5agent4Data Point4Number4
6agent3Data Point5Number5

<tbody>
</tbody>

And this is the table that I need to update. If agent1 appears in column A, row 2, a lookup should find agent1 under John, and populate D2 with "John". Then it should move forward to row 3 and perform the same check for D3, then D4 and so on.
 
Upvote 0
I see what you mean. You'd think that one of the lookup functions would work, but I couldn't figure it out. Creating a custom function was very easy though.

To do that, open your spreadsheet (make a backup first). Press ALT-F11 to open the Visual Basic Editor. On the left navigation pane, right click where it says VBAProject, then select Insert, and Module. When the window opens up, paste this:

Public Function GetSupervisor(agent)
Dim myagent as object

Set myagent = Sheets("teams").Cells.Find(agent)
If myagent Is Nothing Then
GetSupervisor = "??"
Else
GetSupervisor = Sheets("teams").Cells(1, myagent.Column)
End If
End Function


Now go back to your data sheet. In D2 type "=GetSupervisor(A2)". Then copy and paste D2 down the rest of column D.

-Eric
 
Upvote 0
That's Brilliant!!!! Worked like a charm!

I can't thank you enough....I'd tell you how many nights I lost sleep over this one, but you might be sorry you helped me if I did!
 
Upvote 0

Forum statistics

Threads
1,216,124
Messages
6,128,995
Members
449,480
Latest member
yesitisasport

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