问题
i have a range containing the following strings:
step_1, step_10, step_3, step_2
using the following code
input_sh.Activate
With ActiveSheet
.Range("H2:H20").Select
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("H2"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers 'xlSortNormal
With .Sort
.SetRange Range("H2:H20")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
step_10, step_1, step_2, step_3
but i would like to get
step_1, step_2,step_3,step_10
回答1:
I would separate the number into another column, your last + 1, using mid function and sort on that.
Edit: I'm not at my pc but I think your only way to do this is to setup a macro that:
- Filter your sheet by the first 9.
- Cut and insert them before row 2.
- Sort these on their own.
- Then remove the filter and sort the rest as you have above.
回答2:
Your strings have underscore followed by numbers. if that is going to be format of your string you can simply split your string using convert text to columns using "_" as your delimiter. Later you can sort and concatenate to get your sorted list of strings.

Sub Sample()
Columns(1).Copy Columns(3)
Columns("C:C").Select
Selection.TextToColumns Destination:=Range("C1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, Other:=True, OtherChar:="_", FieldInfo:=Array(Array(1, 1), Array(2, 1))
Columns("D:D").Sort Range("D1")
i = 1
Do While Not IsEmpty(Range("C" & i))
Range("B" & i) = Range("C" & i) & "_" & Range("D" & i)
i = i + 1
Loop
End Sub
回答3:
thanks every one for you contribution. to user I found my solution before reading your suggestion. thanks anyway for your effort
my solution:
- split str for "_"
- write 2nd index next to filenames order 2nd cols by then col with only number
- clean col with numbers
来源:https://stackoverflow.com/questions/15110831/sorting-range-in-ascending-numerical-order-containing-strings-by-vba-excel