+ Reply to Thread
Results 1 to 14 of 14

Sort data 2nd rows

Hybrid View

  1. #1
    Registered User
    Join Date
    06-24-2013
    Location
    Kosice
    MS-Off Ver
    Excel 2003
    Posts
    53

    Sort data 2nd rows

    Hi, My english is not good sorry :P


    I have problem, I want sort data by 2nd line (rows )

    example


    1 surname#1
    2 name#1
    3 surname#2
    4 name#2
    5 surname#3
    6 name#3

    I want sorting only by surname, I dont know how

    thank you for help
    Last edited by Marianus; 06-24-2013 at 09:18 AM.

  2. #2
    Registered User
    Join Date
    06-24-2013
    Location
    Kosice
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: Sort data 2nd rows

    heeeeeeeeeeelp

  3. #3
    Valued Forum Contributor
    Join Date
    10-26-2008
    Location
    Birmingham, UK
    MS-Off Ver
    All versions up to 2010
    Posts
    1,025

    Re: Sort data 2nd rows

    Hi
    See sample workbook. Formulas are in every other cell in column B. Then you can sort by surname.
    Hope this helps.
    Good luck.
    Tony
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Sort data 2nd rows

    I would suggest joining the surnames and first names together and then sorting and "unjoin" them afterwards. Something like:
    Sub macro_1()
    Dim count As Integer, max_count As Integer
    count = 1
    Do Until Range("A" & count) = ""
        Range("A" & count) = Range("A" & count) & "," & Range("A" & count + 1)
        Range("A" & count + 1) = ""
        count = count + 2
    Loop
    max_count = count
    Range("A1:A" & max_count).Sort Key1:=Range("A1"), Order1:=xlAscending
    count = 1
    Do Until count >= max_count
        Rows(count + 1).Insert
        Range("A" & count + 1) = Right(Range("A" & count), Len(Range("A" & count)) - InStr(1, Range("A" & count), ","))
        Range("A" & count) = Left(Range("A" & count), InStr(1, Range("A" & count), ",") - 1)
        count = count + 2
    Loop
    End Sub
    (based on your data being in column A, starting at row 1)

  5. #5
    Registered User
    Join Date
    06-24-2013
    Location
    Kosice
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: Sort data 2nd rows

    thank you but not work I want , as I thought

    I need surname in 1st line a in 2nd name for tables

    1 surname#1
    2 name#1
    3 surname#2
    4 name#2
    5 surname#3
    6 name#3

    - sort.xlsm

    I know sort this :

    1 surname#1 / name#1
    2 surname#2 / name#2
    3 surname#3 / name#3


    ... its easy
    Last edited by Marianus; 06-24-2013 at 09:55 AM.

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Sort data 2nd rows

    That's much more difficult. Especially as you are throwing in merged cells! You'll have to give me a minute to come up with a solution.

  7. #7
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Sort data 2nd rows

    Ok, I've come up with this:
    Option Explicit
    Sub macro_1()
    Dim wS1 As Worksheet, wS2 As Worksheet, count As Integer, count_2 As Integer
    Set wS1 = ActiveSheet
    Set wS2 = Sheets.Add
    count = 3
    count_2 = 3
    Do Until wS1.Range("A" & count) = ""
        wS2.Range("A" & count_2) = wS1.Range("A" & count)
        wS2.Range("B" & count_2) = wS1.Range("A" & count + 1)
        wS2.Range("C" & count_2) = wS1.Range("B" & count)
        wS2.Range("D" & count_2) = wS1.Range("B" & count + 1)
        wS2.Range("E" & count_2) = wS1.Range("C" & count)
        wS2.Range("F" & count_2) = wS1.Range("D" & count)
        wS2.Range("G" & count_2) = wS1.Range("D" & count + 1)
        wS2.Range("H" & count_2) = wS1.Range("E" & count)
        count = count + 2
        count_2 = count_2 + 1
    Loop
    wS2.Range("A3:H" & count).Sort Key1:=Range("A3"), Order1:=xlAscending
    count = 3
    For count_2 = 3 To count_2
        wS1.Range("A" & count) = wS2.Range("A" & count_2)
        wS1.Range("A" & count + 1) = wS2.Range("B" & count_2)
        wS1.Range("B" & count) = wS2.Range("C" & count_2)
        wS1.Range("B" & count + 1) = wS2.Range("D" & count_2)
        wS1.Range("C" & count) = wS2.Range("E" & count_2)
        wS1.Range("D" & count) = wS2.Range("F" & count_2)
        wS1.Range("D" & count + 1) = wS2.Range("G" & count_2)
        wS1.Range("E" & count) = wS2.Range("H" & count_2)
        count = count + 2
    Next
    Application.DisplayAlerts = False
    wS2.Delete
    Application.DisplayAlerts = True
    End Sub
    I've tested it against the sample you provided and it seems to work.

  8. #8
    Registered User
    Join Date
    06-24-2013
    Location
    Kosice
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: Sort data 2nd rows

    thank you , but is error when is end I dont understand why

  9. #9
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Sort data 2nd rows

    What is the error and where does it occur? I've just double-checked/tested it again and it still gave me the correct result for your sample.

    Are you testing it on the sample or the full data?

  10. #10
    Registered User
    Join Date
    06-24-2013
    Location
    Kosice
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: Sort data 2nd rows

    look for this - sort.vA02.xlsm

  11. #11
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Sort data 2nd rows

    The macro I gave you works on the data in sort.vA02.xlsm on my machine. Try putting it in a new module and running it from there rather than the command button? What is the error you get? Is it an error in the code? Or is the end result not what you want?

  12. #12
    Registered User
    Join Date
    06-24-2013
    Location
    Kosice
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: Sort data 2nd rows

    I work with command button , its my weakness, I use button to action and there si errror

  13. #13
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Sort data 2nd rows

    Marianus, try changing this line in the code
    wS2.Range("A3:H" & count).Sort Key1:=wS2.Range("A3"), Order1:=xlAscending

  14. #14
    Registered User
    Join Date
    06-24-2013
    Location
    Kosice
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: Sort data 2nd rows

    Thank you for all

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1