Link sort variable to a message/input box

gabe

New Member
Joined
Jul 30, 2008
Messages
33
Hello, I'm not terribly experienced with VBA and was hoping that someone could point me in the right direction regarding something I'm trying to do. What I want to do is sort a list of job numbers in ascending order unless I specify the specific job codes that I want to come first in my list. I’ve got the code below which basically works but I'm not sure how to link the inputs highlighted in red to a message box in order to allow me to vary the input each time I run the code. It would be great if someone has a clear example as to how I can do this. I know this should be easy to do. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
Also, would it be possible to loop through the sort input for multiple groupings of job codes? <o:p></o:p>


Sub SortAndInsertRowsTest2()
Dim Rng As Range, Dn As Range
Dim Lrow As Long, i As Long
Set Rng = Range(Range("A2"), Cells(2, Columns.Count).End(xlToLeft))
'
' Sort by custom list then job number
'
LR = Cells(Rows.Count, 4).End(xlUp).Row
Application.AddCustomList ListArray:=Array("2890", "2620", "2000")
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:= _
Range("C3:C" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder _
:="2890,2620,2000", DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange Range("A2:X" & LR)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Something like this for your first question:
Code:
Sub xyzzy()

    Dim sInput As String
    Dim jobCodesArray As Variant
    
    sInput = InputBox("Enter job codes separated by commas")
    
    If sInput <> "" Then
        jobCodesArray = Array(Split(sInput, ","))
        Application.AddCustomList ListArray:=jobCodesArray
    End If
        
End Sub
 
Upvote 0
John, I've been on vacation and just got your reply. I tried to incorporate your code below but I don't think I've got it quite right. Can you take a look and let me know what I've done wrong?

Sub SortAndInsertRowsTest3()
Dim Rng As Range, Dn As Range
Dim Lrow As Long, i As Long
Dim sInput As String
Dim jobCodesArray As Variant
Set Rng = Range(Range("A2"), Cells(2, Columns.Count).End(xlToLeft))
'
' Sort by jobnbr & custom list
'
sInput = InputBox("Enter job codes separated by commas")
If sInput <> "" Then
jobCodesArray = Array(Split(sInput, ","))
Application.AddCustomList ListArray:=jobCodesArray
End If

LR = Cells(Rows.Count, 4).End(xlUp).Row
Application.AddCustomList ListArray:=Array("jobCodesArray")
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:= _
Range("C3:C" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder _
:="jobCodesArray", DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange Range("A2:X" & LR)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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