Button Sort Code

bpgolferguy

Active Member
Joined
Mar 1, 2009
Messages
469
Hi, I currently have this code in a button:

Code:
Private Sub TourPlayers_Click()
    With Workbooks("MemberDatabase.xls").Worksheets("TourPlayers")
    .Range("A2:E300").ClearContents
    End With
    'First Name
    With Workbooks("MemberDatabase.xls").Worksheets("Members")
    .Range("A4").Resize(.Range("AD1").Value).Copy
    End With
    With Workbooks("MemberDatabase.xls").Worksheets("TourPlayers")
    .Range("C2").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    End With
    'Last Name
    With Workbooks("MemberDatabase.xls").Worksheets("Members")
    .Range("B4").Resize(.Range("AD1").Value).Copy
    End With
    Worksheets("TourPlayers").Range("D2").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    'KJGT Division
    With Workbooks("MemberDatabase.xls").Worksheets("Members")
    .Range("D4").Resize(.Range("AD1").Value).Copy
    End With
    Worksheets("TourPlayers").Range("E2").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    'M/W Division
    With Workbooks("MemberDatabase.xls").Worksheets("Members")
    .Range("F4").Resize(.Range("AD1").Value).Copy
    End With
    Worksheets("TourPlayers").Range("F2").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    With Workbooks("MemberDatabase.xls").Worksheets("TourPlayers")
    .Range("A2:F400").Select
    Selection.Sort Key1:=Range("F2"), Order1:=xlAscending, Key2:=Range("E2") _
    , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
    False, Orientation:=xlTopToBottom
    Range("AD2").Select
    End With
End Sub
The very last part where I sort is what's giving me fits. Starting with ".Range("A2:F400") is what it highlights and gives me an error when I click the button. Anyone know why?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Why not just remove the Select/Selection?
Code:
    With Workbooks("MemberDatabase.xls").Worksheets("TourPlayers")
        .Range("A2:F400").Sort Key1:=.Range("F2"), Order1:=xlAscending, Key2:=.Range("E2") _
                                                                              , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
                               False, Orientation:=xlTopToBottom
        Application.Goto .Range("AD2")
    End With
 
Upvote 0
Yes, right now in the design phase I haven't narrowed down exactly how many rows may or may not be populated. Once I get closer to being finished with it, I will know and can therefore make all of those figures consistent. Thanks!
 
Upvote 0
Hi, I have another one that is causing me problems and not working.....this is the part of the code it highlights when I click the button

With Workbooks("MemberDatabase.xls").Worksheets("TourPlayers")
.Range("A2:F400").Sort Key1:=.Range("E2"), Order1:=xlAscending, Key2:=Range("F2") _
, Order2:=xlAscending, Key3:=Range("G2") _
, Order3:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With

It says the sort reference is not valid.
 
Upvote 0
Code:
.Range("A2:F400").Sort Key1:=[SIZE=5][COLOR=#ff0000].[/COLOR][/SIZE]Range("E2"), Order1:=xlAscending, _
                       Key2:=[SIZE=5][COLOR=red].[/COLOR][/SIZE]Range("F2"), Order2:=xlAscending, _
                       Key3:=[SIZE=5][COLOR=#ff0000].[/COLOR][/SIZE]Range("G2"), Order2:=[COLOR=red]????[/COLOR]
 
Last edited:
Upvote 0
I don't think it's the 3 criteria causing the problems, I think it might be some missing dots.

Try sticking a dot before Range for all the keys, eg Key1:=.Range("F2")
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,762
Members
452,940
Latest member
rootytrip

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