Closed Thread
Results 1 to 13 of 13

Macro to sort data in spreadsheet but maintain empty rows between data.

  1. #1
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Question Macro to sort data in spreadsheet but maintain empty rows between data.

    Hello.

    The solution to this might be very simple, but it escapes me. I have a spreadsheet where I want to sort data across the entire spreadsheet according to the value in column A. The problem is that between rows of data I have blank rows and I need to keep it that way.

    When I sort with excel it will sort the column perfectly but it will move the blanks to the very bottom of the column and I need them between the rows of data.

    What would be the best solution for this? Should I do a sort and then have a macro put a space between the rows that have a different value in column A or is there a better way?

    The spreadsheet could look like this:

    COLUMN A:

    ZZ091
    ZZ091
    ZZ091
    Blank row
    XX189
    Blank row
    ZZ092
    Blank row
    XX190
    XX190
    Blank row
    XX191
    XX191

    After running the macro I want it to become:

    XX189
    Blank row
    XX190
    XX190
    Blank row
    XX191
    XX191
    Blank row
    ZZ091
    ZZ091
    ZZ091
    Blank row
    ZZ092

    Please let me know if the question is not clear and I will try to clarify what I am trying to do. Thanks for your help.

  2. #2
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    678

    Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    copy data in sheet1 in sheet2 also for retrieval

    try this macro test

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    I am not an expert. better solutions may be available
    [email protected]

  3. #3
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    Hello venkat1926!

    Thank you very much for your help. It is a great start for me.

    Your code almost works, but I do not think that I was very clear and it does not do everything that I need it to.

    After running your code on this:
    ZZ091
    ZZ091
    ZZ091

    XX189

    ZZ092

    XX190
    XX190

    XX191
    XX191

    I get:

    ZZ091
    XX189

    XX190
    XX190

    XX191
    XX191

    ZZ091
    ZZ091

    ZZ092

    I tried to figure out why the first row is not sorted correctly, but I could not figure it out.

    This is very close to the result I was looking for. However, it does not keep the data in the other columns associated with the data in column A (I do not think I explained this very well, sorry).

    I have several pieces of data in the columns that follow A that needs to move with the data in column A. So for example:

    Column A Column C
    ZZ091 data associated with ZZ091
    ZZ091 data associated with ZZ091
    ZZ091 data associated with ZZ091

    XX189 data associated with XX189

    ZZ092 data associated with ZZ092

    XX190 data associated with XX190
    XX190 data associated with XX190

    XX191 data associated with XX191
    XX191 data associated with XX191

    should become:

    Column A Column C
    XX189 data associated with XX189

    XX190 data associated with XX190
    XX190 data associated with XX190

    XX191 data associated with XX191
    XX191 data associated with XX191

    ZZ091 data associated with ZZ091
    ZZ091 data associated with ZZ091
    ZZ091 data associated with ZZ091

    ZZ092 data associated with ZZ092

    Is this possible to do with a macro? I am really struggling to figure it out.

    Any help is much appreciated.

    Thanks!
    Last edited by vzc8; 02-16-2012 at 10:12 AM.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    Please Login or Register  to view this content.



  5. #5
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    Hello snb,

    Thank you very much for the post. Sorry about the late reply.

    I tried running this code but it does not seem to do anything. Is there anything special that I need to do to make it work?

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    In that case the cells in column A are not empty
    Post a sample workbook.

  7. #7
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    Hello.

    Thanks for the reply. I do not think I explained what I am trying to do very well.

    Here is an excel file with an example of what I am trying to sort and a second tab with an example of what I want the data to look like after the sort.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    Hi, I have been working on this but have made no progress. If anyone has any ideas it would be great. I have been trying to modify venkat1926's code, but have not gotten anywhere.

  9. #9
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    Thanks for the help everyone, sorry about not explaining this situations very well.

    I am guessing that this is harder to do than I expected. I have still not figured it out. I'll try to avoid having to do this instead. I am not sure how to close a thread but if any admin sees it they can feel free to close this.

    Thanks again.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    Try
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    Thanks so much jindon

    This is exactly what I was trying to do. Works perfectly!

  12. #12
    Registered User
    Join Date
    06-11-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    12

    Talking Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    I have a question very similar to this case.

    Original Raw Data Set Consists of 3 different product types followed by a unique number
    Product types are ZB, ZR and ZC
    Column A = Sales Number
    Column B = Profit

    Column A:
    ZB12221
    ZB12231
    ZR12241
    ZB12251
    ZR12261
    ZC12271
    ZB12281

    Column B:
    $5
    $6
    $4
    $5


    End Result Goal"
    ZB12221
    ZB12231
    ZB12251
    SPACE // SUM OF PROFIT
    ZR12241
    ZR12261
    SPACE // SUM OF PROFIT
    ZC12271
    SPACE // SUM OF PROFIT

    Sort; space between differing product types and sum the column B values.

    Any answers out there?

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Macro to sort data in spreadsheet but maintain empty rows between data.

    maxutility,

    Hi-jacking someone else' thread is against the rule here.

    Open a new thread for your own and link to this thread.
    You will probably be asked to upload a sample workbook to work with.

Closed 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