+ Reply to Thread
Results 1 to 3 of 3

Difficult sort problem

Hybrid View

  1. #1
    Registered User
    Join Date
    05-07-2014
    Location
    Prescott, AZ
    MS-Off Ver
    Excel 2007
    Posts
    35

    Difficult sort problem

    I have a schedule program that uses one cell, (A1) lets say, and then corresponding to that persons name in A1 there is schedule data in B1:W2.

    When I add a new employee or delete one, I need to re sort the list to accommodate the change and keep all scheduling information in sync.

    My key column will be A, but data is only in every other cell. A1, A3, A5....A(n)

    If I use a regular sort, all of the blank cells A2, A4, A6... will be sorted out and I will have two employee names in the space only one should be. (A1 and A2 instead of just A1 and a blank in A2)

    Left column is what it is, right column is what I would get with a regular sort and it needs to be a name then a space, the next name and then a space.....


    Alex Alex
    Ben
    Ben Charles
    Dave
    Charles Edward
    Frank
    Dave

    Edward

    Frank

    some help with a sort routine that would work would be awesome.

    thanks

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,698

    Re: Difficult sort problem

    This code will insert a blank row between each row.

    Option Explicit
    
    Sub Insert()
    Dim lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Dim rw As Variant
    Dim rng As Range
    Set rng = Range("A1:A" & lr)
    
    
    For Each rw In rng.Rows 'For each row in your range...
        If rw.Row Mod 2 = 0 Then 'check to see if the row is even
            rw.Insert Shift:=xlDown 'if the row is even, insert a blank row above it
        End If
    Next rw 'Keep going until you are out of rows
    
    
    End Sub
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Difficult sort problem

    Try this macro:

    Sub test()
    Dim lastr As Long, cell As Range
    lastr = Cells(Rows.Count, "A").End(xlUp).Row + 1
    
    Columns("A:A").Insert Shift:=xlToRight
    Range("A1").Value = 1
    Range("A1:A" & lastr).DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1, Trend:=False
    Range("A1:X" & lastr).Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlNo
    Columns("A:A").Insert Shift:=xlToRight
    Range("A1").Value = 1
    Range("A1:A" & lastr / 2).DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=2, Trend:=False
    Range("A1:Y" & lastr).Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlNo
    For Each cell In Range("A2:A" & lastr)
      If cell = "" Then cell = cell.Offset(-1, 0) + 1
    Next cell
    Range("A1:Y" & lastr).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo
    Columns("A:B").Delete Shift:=xlToLeft
    End Sub
    Hope it is easy to read that
    1) in inserted columns are inserted consequtive numbers,
    2) then data is sorted on name from column ex-A (now B).
    3) first half (ex. A5 A1 A3 etc.) is then numbered in new column (with step 2)
    4) and all data is again sorted on numbers from point 1 (original sequence resored)
    5) rows with no data in column A are assigned next number after the one from above
    6) everything is sorted on column A
    7) no longer needed (inserted) columns A and B are deleted
    Best Regards,

    Kaper

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Difficult V Look up and if function together problem
    By tentop6521 in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 01-23-2014, 04:00 PM
  2. [SOLVED] A difficult sort
    By Obfuscated in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-12-2013, 06:32 PM
  3. difficult filter/sort
    By Tomas mcD in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-30-2011, 04:36 PM
  4. [SOLVED] a difficult problem
    By x taol in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2006, 02:19 AM
  5. [SOLVED] Difficult Sorting Problem
    By Rob in forum Excel General
    Replies: 2
    Last Post: 01-05-2005, 12:06 PM

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