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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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,214,567
Messages
6,120,268
Members
448,953
Latest member
Dutchie_1

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