Sorting range in ascending numerical order containing strings by vba excel

后端 未结 3 1270
不知归路
不知归路 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 11:56

    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
    0 讨论(0)
  • 2021-01-25 12:03

    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:

    1. Filter your sheet by the first 9.
    2. Cut and insert them before row 2.
    3. Sort these on their own.
    4. Then remove the filter and sort the rest as you have above.
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题