Sort code in button not working

bpgolferguy

Active Member
Joined
Mar 1, 2009
Messages
469
Hi, I have this code inside a button on workbook:

Code:
Private Sub CommandButton5_Click()
Range("C5:F300").Copy
Sheets("Overall").Range("B3").PasteSpecial Paste:=xlValues
With Sheets("TourSBAmMen")
Application.CutCopyMode = False
End With
Sheets("Overall").Range("B3:E300").Sort Key1:=Range("D3"), Order1:=xlDescending, Key2:=Range("E3"), Order2:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
Range("A2").Select
End Sub

It works okay, except it doesn't sort the range I've chosen on sheet "Overall". Anyone know why? Thanks!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try

Code:
Private Sub CommandButton5_Click()
Range("C5:F300").Copy
Sheets("Overall").Range("B3").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
With Sheets("Overall")
    .Range("B3:E300").Sort Key1:=.Range("D3"), Order1:=xlDescending, Key2:=.Range("E3"), Order2:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
End With
End Sub
 
Upvote 0
Have those dots gone astray again?

Don't know if this helps, but you don't have to use With all the time.:)
Code:
Private Sub CommandButton5_Click()
 
Dim wsOveralll As Worksheet
Dim wsTour As Worksheet
 
    Set wsOverAll = Sheets("Overall")
 
    Set wsTour = Sheets("TourSBAmMen")
 
    wsTour.Range("C5:F300").Copy
 
    wsOverAll.Range("B3").PasteSpecial Paste:=xlValues
 
    Application.CutCopyMode = False
 
    wsOverAll.Range("B3:E300").Sort Key1:=wsOverAll.Range("D3"), Order1:=xlDescending, Key2:=wsOverAll.Range("E3"), Order2:=xlAscending, Header:=xlGuess, _
                                    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                                    DataOption1:=xlSortNormal
 
End Sub
 
Upvote 0
Thanks! One last thing....is there a reason it's not including row 3 in this sort? Either I don't have something right, or it is sorting from row 4 down....any thoughts?
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
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