Sort

  • Thread starter Thread starter N. McCain
  • Start date Start date
N

N. McCain

I need a command button to sort the entire sheet by column "A" in ascending
order. My button is only sorting column "A" and nothing else. Can someone
please help me? Thanks in advance
 
Private Sub CommandButton1_Click()
Range("a3").Activate
Range("a3:a4000").sort Key1:=Range("a3")
CommandButton1.Activate
End Sub

I need to be able to sort 4000 rows and columns A-L.
 
Also I need it to start sorting at row 3

N. McCain said:
Private Sub CommandButton1_Click()
Range("a3").Activate
Range("a3:a4000").sort Key1:=Range("a3")
CommandButton1.Activate
End Sub

I need to be able to sort 4000 rows and columns A-L.
 
change the a4000 to b4000, c4000 or whatever the last column you want to include
in the sort.
 
I have figured out how to get it all to sort thanks to Don pointing out that
I needed to try and select the entire sheet. Below is my code. However, I
have one more question/problem. I need this sheet to be protected. When I
protect the sheet and then click the button, I get an error. When the sheet
is unprotected, it will sort just fine. What code, if any, will help me sort
this while the sheet is protected?

Private Sub CommandButton1_Click()
Range("a3").Activate
Range("a3:L4000").sort Key1:=Range("a3:l4000")
CommandButton1.Activate
End Sub
 
I finally got it to work. Here is some code to help anyone who may need it.
This will allow you to sort the entire sheet ascending by column A. It will
sort 4000 rows and through column L starting at row 3. My sheet is
protected, but I needed to allow the users to use the auto filter feature.
Thanks for everyone's help with this.

Sub ProtectionOptions()
If ActiveSheet.Protection.AllowFiltering = False Then
ActiveSheet.Protect AllowFiltering:=True
End If
End Sub
Private Sub CommandButton1_Click()
ActiveSheet.Unprotect
Range("a3").Activate
Range("a3:L4000").Sort Key1:=Range("a3:l4000")
CommandButton1.Activate
ActiveSheet.EnableAutoFilter = True
ActiveSheet.Protect contents:=True, userInterfaceOnly:=True
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top