Returns values based on one cell

nkroberts

New Member
Joined
Mar 5, 2015
Messages
18
On sheet 1 I have a drop down in cell A1 that lists 4 teams. On sheet 2 I have a list of athletes (column B) their team (Column A) and their score (Column C).

Depending on what is selected in cell A1 I want to list their top ten athletes (A2:A11) and their scores (B2:B11)
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
alter the sheet names as you wish:

Code:
Sub TopSort()
Dim iRows As Long, iStart As Long, iLast As Long
Dim shtTarg As Worksheet
Dim sTeam


sTeam = Range("A1").Value


Sheets("Teams").Activate
    Range("A1").Select
    Cells.Find(What:=sTeam, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    iStart = ActiveCell.Row
    While ActiveCell.Value = sTeam
        ActiveCell.Offset(1, 0).Select
    Wend
    iLast = ActiveCell.Row - 1
    iRows = iLast - iStart + 1
    
    Range("A" & iStart & ":C" & iLast).Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    Set shtTarg = ActiveSheet
    
    ActiveSheet.Paste
    Application.CutCopyMode = False
    shtTarg.Sort.SortFields.Clear
    shtTarg.Sort.SortFields.Add Key:=Range("C1:C" & iRows), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With shtTarg.Sort
        .SetRange Range("A1:C" & iRows)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
    ActiveSheet.Name = sTeam
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,116
Messages
6,189,057
Members
453,524
Latest member
AshJames

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