I need to sort a list useing a macro. I need to promp for the field to be sorted, and display a messageBox if the field name is not rite.
Macro to prompt for field name then sort the list
-
-
No help? thats ok I got it to work.
Code
Display MoreSub Sort_Data() ' ' Sort_Data Macro ' Macro recorded 9/18/2006 ' ' sortName = InputBox("Enter To, M, T, S, O, P, Or Tr", "Select Sort Key") If sortName = "To" Or _ sortName = "M" Or _ sortName = "T" Or _ sortName = "S" Or _ sortName = "O" Or _ sortName = "P" Or _ sortName = "Tr" _ Then Range("A1:G26").Sort Key1:=Range(sortName), Order1:=xlAscending, Header:= _ xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal ElseIf sortName <> " " Then TryAgain = MsgBox("Please Enter To, M, T, S, O, P, Or Tr", vbExclamation, "Sort key not valid") End If End Sub
-
Re: Macro to prompt for field name then sort the list
I'm not sure how you expected help after offering so little information. However, I'm pleased to see you shared your solution.
This might be a bit better:
Code
Display MoreOption Explicit Sub Sort_Data() Dim rData As Range Dim sSortName As String Set rData = Range("A1", Range("G65536").End(xlUp)) 'make the range dynamic sSortName = InputBox("Enter To, M, T, S, O, P, Or Tr", "Select Sort Key") Select Case sSortName Case "To", "M", "T", "S", "O", "P", "Tr" rData.Sort Key1:=Range(sSortName), Order1:=xlAscending, Header:= _ xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal Case Else Select Case MsgBox("Please Enter To, M, T, S, O, P, Or Tr", vbYesNo Or vbQuestion Or vbDefaultButton1, "Sort key not valid") Case vbYes Call Sort_Data Case vbNo Exit Sub End Select End Select End Sub
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!