+ Reply to Thread
Results 1 to 6 of 6

Macro for Sorting a Variable amount of Rows

Hybrid View

  1. #1
    Registered User
    Join Date
    03-04-2009
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2003
    Posts
    14

    Macro for Sorting a Variable amount of Rows

    Hi All,

    I'm looking to be able to create a macro that will sort ascending a variable amount of rows. The form, in order to get a picture of what it looks like, is comprised of a variable number of invoices, extracted into Excel ('03) from another system. Each invoice also contains a variable amount of rows.

    Let's say the numbers I would like to sort ascending are in Column U and the macro will get to the first line in this grouping via cell referencing. In this case, let's say the first line is Row 7 and there are 5 rows in the first invoice. Therefore, using cell referencing, I would have to sort ascending the range of A1:A5 (without cell referencing, U7:U12).

    My two questions are as follows:
    1) How can I create this variable range (i.e.
    ActiveCell.Range("A1:A5").Select
    ), when A5 is variable

    2) How can I create the entire variable range of the area it will sort ascending with it, expanding the selection to the entire block I'm working with, which will also vary according to top and bottom rows, but not columns

    I have screwed around with vRowBottom, vRowTop, vDiff=vRowBottom - vRowTop, and then referencing vDiff in the Range, but I'm not quite sure how to do this with sorting. If I was using a formula such as sum, it would look something like this:
    Selection.FormulaR1C1 = "=SUM(R[" & -vDiff & "]C:R[-1]C)"
    (just as an example).

    Please let me know if anyone has any thoughts and if you have any questions or need me to provide any further information, just let me know.
    Last edited by VBA Noob; 04-15-2009 at 01:34 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro for Sorting a Variable amount of Rows

    Hello stepheng2383,

    You need to post a sample workbook. If you have confidential information, change it before posting the workbook. If you are unable to change the workbook then provide samples that are functionally equivalent to the original workbook. These sample should be in a workbook if you are using worksheet formulas. Neither you or any of us want to recopy formulas into cells.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-04-2009
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Macro for Sorting a Variable amount of Rows

    I have attached a sample workbook (Excel '03) for your information, to see exactly what I'm referring to. I have also added in a comment in the "Agreement #" column. If you have any follow-up questions, please let me know. Thanks.
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro for Sorting a Variable amount of Rows

    Hello stepheng2383,

    I take the agreement number and invoice number are the same thing. So, you want the line items copied over to another worksheet in ascending order by invoice number, and the total lines are to be ignored?

  5. #5
    Registered User
    Join Date
    03-04-2009
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Macro for Sorting a Variable amount of Rows

    The agreement number and invoice number are the same. I'm looking to sort the lines in each invoice (i.e. the first group of seven lines with data, making up the first invoice) in place by agreement number.

  6. #6
    Registered User
    Join Date
    03-04-2009
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2003
    Posts
    14

    Question Re: Macro for Sorting a Variable amount of Rows

    Still in complications attempting to resolve this. If anyone has any ideas or would like further explanation of what I'm attempting to do, please let me know. Anything and everything would be greatly appreciated. Thanks!

+ 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