+ Reply to Thread
Results 1 to 6 of 6

VBA code to sort a dataset with multiple columns based on a variable number of rows

  1. #1
    Registered User
    Join Date
    03-18-2019
    Location
    Toronto, Canada
    MS-Off Ver
    MS 2019 (Mac)
    Posts
    3

    VBA code to sort a dataset with multiple columns based on a variable number of rows

    Hi everyone,

    I'm trying to build a macro that will automatically sort a full selection dataset upon opening the workbook in Excel. I'm having trouble defining the range because the number of rows within the dataset would vary depending on the file (but would all start in the same format, i.e., one file could have rows 5-10, another could have rows 5-25, but no spaces in between the data).

    Input:
    Cell A5 -> B5 -> C5
    Category. A. B.
    Category A 1% 90%
    Category B 2% 70%
    Category C 3% 80%
    Category D 4% 55%

    Desired output (sort "B." by ascending):
    Cell A5 -> B5 -> C5
    Category. A. B.
    Category D 4% 55%
    Category B 2% 70%
    Category C 3% 80%
    Category A 1% 90%

    Any help would be appreciated!

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,815

    Re: VBA code to sort a dataset with multiple columns based on a variable number of rows

    The manner in which you posted your data is difficult to read. It looks like your data starts in row 5. Is that correct? Do you have headers in row 4? Can you more clearly describe what data is in column A, Column B and column C?
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    03-18-2019
    Location
    Toronto, Canada
    MS-Off Ver
    MS 2019 (Mac)
    Posts
    3

    Re: VBA code to sort a dataset with multiple columns based on a variable number of rows

    Sorry, first time poster. I've attached a screenshot of the input... the selection always starts at A5 (row 5 is for headers) and has those 4 specific columns... it would be re-sorting the whole dataset by "positive" based on ascending order (where it's currently set up on descending order).

    Screen Shot 2019-03-18 at 1.55.24 PM.png

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,815

    Re: VBA code to sort a dataset with multiple columns based on a variable number of rows

    I'm sorry but I can't view the screenshot for some reason. Can you attach a copy of your file? Click the "Go Advanced" button at the bottom right. Then scroll down a bit and click "Manage Attachments". This should allow you to attach your file.

  5. #5
    Registered User
    Join Date
    03-18-2019
    Location
    Toronto, Canada
    MS-Off Ver
    MS 2019 (Mac)
    Posts
    3

    Re: VBA code to sort a dataset with multiple columns based on a variable number of rows

    This should work - thanks for the direction!
    Attached Images Attached Images

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,815

    Re: VBA code to sort a dataset with multiple columns based on a variable number of rows

    Try:
    Please Login or Register  to view this content.

+ 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. [SOLVED] Copy multiple columns x number of times based on variable number?
    By nobodyukno in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-19-2017, 04:13 PM
  2. [SOLVED] Count number of occurrences based on criteria in multiple columns and rows
    By gerryger in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-22-2015, 08:13 PM
  3. [SOLVED] Delete rows based on the value of two columns using VBA code - Multiple worksheets
    By OmniBlue in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-18-2015, 04:24 PM
  4. How to concatenate a variable number of rows/cells based on multiple criteria?
    By jonharadon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-16-2012, 09:22 PM
  5. Replies: 0
    Last Post: 10-16-2012, 03:26 PM
  6. [SOLVED] Group/sort rows based on values in multiple columns?
    By teitoku in forum Excel General
    Replies: 5
    Last Post: 08-07-2012, 02:20 PM
  7. Replies: 6
    Last Post: 04-14-2012, 04:07 AM

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