+ Reply to Thread
Results 1 to 3 of 3

Excel Align equal values in adjoining Columns

  1. #1
    Registered User
    Join Date
    09-11-2006
    Posts
    2

    Excel Align equal values in adjoining Columns

    Hi all,
    I was hoping someone could help me out on sorting values in Excel.
    This is what i would like to do:

    I have a group of values in column A ie:

    1
    2
    3
    4
    5

    And also another group of values in column B ie:

    8
    2
    4
    6
    5

    and i want to sort the values from both columns like this:

    Column A Column B
    1
    2 2
    3
    4 4
    5 5

    8
    6

    I want to leave the equal values side by side where they match.
    Thanks in advance,
    Pat

  2. #2
    Valued Forum Contributor
    Join Date
    12-16-2004
    Location
    Canada, Quebec
    Posts
    363
    Hi Pat

    Maybe this can help out
    Sub main()
    Call sort_range
    rowcount = Cells(Cells.Rows.Count, "a").End(xlUp).Row
    For i = 1 To rowcount
    Range("a" & i).Select
    firstcell = ActiveCell
    firstcell = Trim(firstcell)
    Range("b" & i).Select
    secondcell = ActiveCell
    secondcell = Trim(secondcell)
    If firstcell <> secondcell Then
    Selection.Insert Shift:=xlDown
    End If
    Next
    Call reformat_column
    End Sub

    Sub sort_range()
    rowcount = Cells(Cells.Rows.Count, "a").End(xlUp).Row
    Range("A1:A" & rowcount).Select
    Range("A" & rowcount).Activate
    Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

    Range("B1:B" & rowcount).Select
    Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End Sub

    Sub reformat_column()
    On Error Resume Next
    rowcount = Cells(Cells.Rows.Count, "b").End(xlUp).Row
    For j = 1 To rowcount
    Range("b" & j).Select
    test_val_b = ActiveCell.Value
    Range("a" & j).Select
    test_val_a = ActiveCell.Value

    If IsEmpty(test_val_a) And test_val_b > 0 Then
    Range("b" & j).Select
    valb_move_a = ActiveCell.Value
    ActiveCell.ClearContents
    ActiveCell.Offset(0, -1).Select
    ActiveCell.Value = valb_move_a
    End If
    Next
    End Sub

  3. #3
    Registered User
    Join Date
    09-11-2006
    Posts
    2

    Thanks

    Thanks that works great!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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