Sort using an input box to select the column

lmmay

Board Regular
Joined
Jun 6, 2014
Messages
87
I am using the following to sort an excel sheet:

Sub Sort()
'Unprotect Worksheet
ActiveSheet.UnProtect Password:=" cover700"
Selection.Sort Key1:=Range(Selection.Address), Order1:=xlAscending, DataOption1:=xlSortNormal
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Password:=" cover700", Scenarios:=True _
, AllowFormattingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True
End Sub

Except for the last line, the code works well sorting on column 1.
I need to modify the script to allow the user to select a different column to sort using an input box.

Any help would be greatly appreciated.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this

Code:
Sub Sort()
'Unprotect Worksheet
    Dim waddress As Variant
    ActiveSheet.Unprotect Password:=" cover700"
    waddress = Application.InputBox("Select column", "SORT", Selection.Address, , , , , 8)
    If waddres = "" Then Exit Sub
    Selection.Sort Key1:=Range(waddress & 1), Order1:=xlAscending, DataOption1:=xlSortNormal
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Password:=" cover700", Scenarios:=True _
    , AllowFormattingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True
End Sub
 
Upvote 0
Solution
The routine exits at If waddress=""

Sorry my mistake.
Try this and tell me.

Code:
Sub Sort()
'Unprotect Worksheet
    Dim waddress As Variant
    ActiveSheet.Unprotect Password:=" cover700"
    waddress = Application.InputBox("Select column", "SORT", Selection.Address, , , , , 8)
    If waddress = False Then Exit Sub
    Selection.Sort Key1:=Range(waddress & 1), Order1:=xlAscending, DataOption1:=xlSortNormal
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Password:=" cover700", Scenarios:=True _
    , AllowFormattingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,749
Messages
6,180,725
Members
452,995
Latest member
isldboy

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