Solved

Excel 2007: Pull data from multiple cells based on a value

Posted on 2011-09-14
15
286 Views
Last Modified: 2012-05-12
Hello,

I need some help with an Excel 2007 spreadsheet. (see attached)

I have 2 worksheets that I am using to automatically populate a third worksheet.

Here is a description of the worksheets:

Employees:
List of employee names and info as well as the group number they are assigned to.

Manager:
List of Managers and their assistants as well as the group number the manager is responsible for.

Teams:
Each group is populated based on the data entered in the other 2 worksheets.

The problem I am having is that I would like to pull all of the names from the Employees worksheet based on the group number they are assigned.

Here is the function I am using but it is not working:

=Employees!$A$5:$A$17

Any assistance that can be offered would be appreciated.

Thank you.
People.xlsx
0
Comment
Question by:baxleyb
  • 9
  • 6
15 Comments
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36536607
I don't see how you could achieve this without using some VBA code to populate the list of Employees - are you happy to have a button on the sheet to pull in the lists?  You can do the looking up of the Group managers and and assisttants using normal formulas.
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36536789
I have written a small piece of code here to do what I think you're asking. I have put a command button on the attached spreadsheet, and changed your manager and assistant lookups to be more flexible.  For eaxmple, if your groups had names rather than numbers, the lookups would still work. You'll notice I've had to make your file macro enabled (.xlsm) to get this working.

Private Sub btnPopulate_Click()

    Dim rngUsed As Excel.Range
    Dim rngCol As Excel.Range
    Dim rngOut As Excel.Range
    Dim c As Excel.Range
    Dim strGroup As String
    
    Dim rngEmps As Excel.Range
    
    Set rngUsed = ActiveSheet.UsedRange
    
    Set rngEmps = ActiveWorkbook.Worksheets("Employees").UsedRange
    Set rngEmps = rngEmps.Offset(1, 0)  ' ingnore first row which has headings
    
    ' ignore the first column
    Set rngUsed = rngUsed.Offset(0, 1)

    For Each rngCol In rngUsed.Columns
        ' employee data starts in the 5th row - change this if it changes
        Set rngOut = rngCol.Cells(5, 1)
        rngCol.Offset(4, 0).Resize(rngCol.Rows.Count - 4, 1).ClearContents
        ' get the group number for this column
        strGroup = rngCol.Cells(2, 1).Value
        
        ' loop through the employees range looking for members of this group
        For Each c In rngEmps.Columns(2).Cells
            If Len(Trim$(c.Value)) > 0 Then
                If c.Value = strGroup Then
                    rngOut.Value = c.Offset(0, -1).Value
                    ' move the output row on
                    Set rngOut = rngOut.Offset(1, 0)
                End If
            End If
        Next c
        
    Next rngCol

End Sub

Open in new window

People-1-.xlsm
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36536850
Sorry just noticed I meant to add a couple of lines after line 33 to quit the loop when the data ends. It's not a big thing but stops it from looking at all the empty cells in the used range for employees.  Overall this whole thing could be done more efficiently, as this searches the whole employee list for each group, but with relatively small amounts of data like this it doesn't have any real impact.  If you actually have 50,000 employees I would reconsider this approach and make sure it only searched the list once, perhaps building up an array of groups.
            Else
                Exit For

Open in new window

0
 

Author Comment

by:baxleyb
ID: 36536979
Hello,

This looks great, thank you.

How do I open the Macro editor? For some reason I cannot find a way to look at the code.

Thank you.
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36537025
Alt-F11 is the keyboard shortcut, or as the code is attached to your Teams worksheet you can right-click on its tab and choose View code from the pop-up menu.
0
 

Author Comment

by:baxleyb
ID: 36538615
Hi,

This looks great, thanks again. Last 2 questions on this.

1. Could the macro also have the ability to sort by alphabetical order?
2. In the Teams worksheet, If a Manager or assistant is in the list of Employees then could those names not show up in the Employees list?

Is this possible?

Thanks.
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36538912
Yes this is possible - give me a few miins to take a look
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 17

Expert Comment

by:andrewssd3
ID: 36539027
Try this: People-1-.xlsm
0
 

Author Comment

by:baxleyb
ID: 36539159
Looks good, the only issue is that when I click Populate the screen appears to be trying to pull the employee data into the teams worksheet. (see screenshot)
issue.bmp
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
ID: 36541322
Yes I noticed that, but I though it was just my screen drivers.  It seems to be the sort causing it. I have fixed this by turning off screen updating, and I have tidied up the sort code so it will work with any number of employees. People-1-.xlsm
0
 

Author Comment

by:baxleyb
ID: 36542734
This looks great, thank you!

Last question.

For future reference, how do you specify the location of the button?

I am looking through the code and don't see a way to do that.

Thanks.
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36542751
The button is placed manually on the worksheet.  If you go into design mode you can just drag it around.  It is an ActiveX control.  It's convenient to have the Developer Tab visible to be able to enter design mode and play around with controls.  If you have Excel 2007 this can be done from Office button...Excel options...Popular...Show developer tab in the ribbon.  If it's 2010 you can show it by right clicking on the ribbon, going to Customize and then checking the Developer tab in the right hand window.
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36573350
Are you happy with this now?  There's quite a bit of effort here and some points would be nice :)
0
 

Author Comment

by:baxleyb
ID: 36582211
I am sorry for the delay, I was out of the office. Thank you ver, very much for your help!

Works perfectly!
0
 

Author Closing Comment

by:baxleyb
ID: 36582222
Excellent job, really helped me to understand how to do something like this in the future.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now