+ Reply to Thread
Results 1 to 7 of 7

Make a macro include whatever number of rows/columns when sorting out

  1. #1
    Registered User
    Join Date
    04-20-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    96

    Make a macro include whatever number of rows/columns when sorting out

    Hi all,

    I recorded a macro to sort out data in reverse order, the only problem I still have, is to make it interactive so it includes and sorts as many columns as the sheet might have. Data can be in just column A, or take as many columns as going to AG, with an always changing number of rows.

    Below is the part of the code I need help with (where I set up the range to Z500 in the meantime), I'd like the macro to include whatever number of rows and columns. I'll appreciate any help!

    ActiveSheet.SORT.SortFields.Clear
    ActiveSheet.SORT.SortFields.Add Key:=Range("A1"), _
    SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

    With ActiveSheet.SORT
    .SetRange Range("A1:Z500")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply

  2. #2
    Forum Contributor
    Join Date
    06-02-2005
    Posts
    100

    Re: Make a macro include whatever number of rows/columns when sorting out

    Please Login or Register  to view this content.
    This will set lrow to the last row of data in column 1 and set lcol to the last column of data in row 1.

    You can change the row/col by changing what cell(x, y) address you reference.

    To get an overall last row and column on a sheet you can instead use:

    Please Login or Register  to view this content.
    Once you have the boundaries defined you can monkey with the ranged references in the sort macro using cell address instead of "A1" references. It takes a bit of poking to adjust the syntax just right but it's doable.

  3. #3
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Make a macro include whatever number of rows/columns when sorting out

    Something like this (Following from what Ouka said above)

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Make a macro include whatever number of rows/columns when sorting out

    woops forgot to do the columns too
    So you can have it in a letter syntax I added a function.

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    06-02-2005
    Posts
    100

    Re: Make a macro include whatever number of rows/columns when sorting out

    Huh, not bad. Never thought to grab the column letter like that.
    I've always screwed around with .Cells(x, y).resize(lrow - x + 1, lcol - y + 1) to set my selection for sorting.

    I'll have to steal that, thanks!

  6. #6
    Registered User
    Join Date
    04-20-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: Make a macro include whatever number of rows/columns when sorting out

    First of all,

    THANK YOU guys so much for the help. I should have stated I'm still learning my way with VBA. I added the last code I was suggested, but guess I'm missing something, 'cause I couldn't make it work.

    I think explaining more the scenario I'm working with would help. I process spreadsheets where the order of rows won't follow any function. Sometimes items can be ordered by visual attributes (like size) or in the order in which an item was received by my dept (such info is NOT included within the spreadsheet). Order changes with each order I process. The point is that, when I have the spreadsheet complete, I need to put all SKUs (column A), in reverse order to then proceed to print labels for all items. What I've been doing, is: a) Inserting a new column A. b)In cell A2, formula =ROW(A2)-1, c)Filling down all rows with the formula. d)Sort by column A, "largest to smallest" and last, e)Selecting column A and delete it. As mentioned before, sometimes my spreadsheet can have a lot of columns, sometimes can be just column A, but rows are always different. Below is the complete current code I have for the entire macro (This was mostly recorded, not typed in, since I'm still new to coding!):

    ' REVERSE Macro

    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A2").Select
    Selection.FormulaR1C1 = "=ROW(RC)-1"
    Selection.Copy
    Range("B2").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(0, -1).Range("A1").Select
    Range(Selection, Selection.End(xlUp)).Select
    ActiveSheet.Paste
    Columns("A:A").EntireColumn.Select

    Application.CutCopyMode = False
    ActiveSheet.SORT.SortFields.Clear
    ActiveSheet.SORT.SortFields.Add Key:=Range("A1"), _
    SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveSheet.SORT
    .SetRange Range("A1:M500")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

    Columns("A:A").Select
    Selection.Delete Shift:=xlToRight
    Range("A2").Select


    End Sub

  7. #7
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Make a macro include whatever number of rows/columns when sorting out

    Your key issue in that code is that you are sorting column A which is a set of identical formula so it doesn't change anything
    you need to update the selection from

    Please Login or Register  to view this content.
    to the range you are using (I mean by adding a row that changes that selection) something like my row

    Please Login or Register  to view this content.
    or more simply

    Please Login or Register  to view this content.
    Although if you are going to add a column you want to reset the last column

    Also you should put the # tags from the tool bar above the window (ie [ CODE ] ) around your code when posting here - it keeps it tidy.
    Last edited by scottiex; 09-24-2015 at 05:26 PM.

+ 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] Editing a Macro to include multiple rows and include text formatting for leading zeros
    By missmea2005 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-04-2014, 09:50 AM
  2. [SOLVED] Filtering macro, count number of rows and include count in last column.
    By Folshot in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-12-2012, 07:17 AM
  3. Macro to include addtional rows/columns
    By namz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-10-2008, 11:47 PM
  4. Make a cell value include number formats
    By Djmask in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-06-2005, 09:06 AM
  5. Make a macro run on rows/columns only if data is present?
    By bpreas - ExcelForums.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-09-2005, 03:05 PM
  6. Replies: 0
    Last Post: 04-28-2005, 07:06 PM
  7. [SOLVED] Excel macro to specify rows and columns to include in printouts
    By Generic Usenet Account in forum Excel General
    Replies: 2
    Last Post: 04-19-2005, 12:06 PM

Tags for this Thread

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