Sorting range in ascending numerical order containing strings by vba excel

后端 未结 3 1300
不知归路
不知归路 2021-01-25 11:22

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
             


        
3条回答
  •  情深已故
    2021-01-25 12:20

    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.

    enter image description here

     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
    

提交回复
热议问题