+ Reply to Thread
Results 1 to 8 of 8

Sorting by Alpha, Adding Subtotal - then removing subtotal - variable data sets

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    7

    Sorting by Alpha, Adding Subtotal - then removing subtotal - variable data sets

    I am trying to build a macro that will add subtotals, sort then remove the subtotals - on variable data set lengths - I have the attached - but when I add it into a larger macro, it fails;

    The part that fails is the sort while the subtotals are on

    I think I need to define a region - or something - but really no idea how to! So far just build by recording and adding on
    Attached Files Attached Files

  2. #2
    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: Sorting by Alpha, Adding Subtotal - then removing subtotal - variable data sets

    You start the code with
    Selection...
    so it depends heavily on what is selected at the beginning.

    The easy cure - should be to add at the top of the code:
    Worksheets("M-Z").activate 'probably not needed, if it's active all the time
    Range("A1:L3000").select 'or other range which you have selected before code starts - may be even just A1
    PS. attaching code inside Word doc is a bit unusual - you can put it in the post (just remember to use code tags as required by our https://www.excelforum.com/forum-rul...rum-rules.html ) or inside attached workbook.
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    10-24-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    7

    Re: Sorting by Alpha, Adding Subtotal - then removing subtotal - variable data sets

    Ok so I have found where it keeps breaking. The below code sorts by subtotalled fields for a specified range - however that range is variable. So when I add in more data, then re run, it does not include all the data in the sort.

    I tried the Range suggestions above and unfortunately still could not get it working. Thanks in advance for help.

    ActiveSheet.Outline.Showlevels RowLevels:=2
    ActiveWorkbook.Worksheet("A-L").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("A-L").Sort.SortFields.Add Key:=Range("L2:L1094"), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("A-L").Sort.SortFields.Add Key:=Range("K2:K1094"), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("A-L").Sort.SortFields.Add Key:=Range("J2:J1094"), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("A-L").Sort.SortFields.Add Key:=Range("I2:I1094"), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("A-L").Sort.SortFields.Add Key:=Range("H2:H1094"), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("A-L").Sort
            .SetRange Range("A1:N1094")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply

  4. #4
    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: Sorting by Alpha, Adding Subtotal - then removing subtotal - variable data sets

    Hi,

    I assume that using http://www.excelforum.pl/download.htm?id=69841 I could find what changes / how "range is variable"

    So the first suggestion is that data is added or removed at the bottom of the range, column A has some kind of index/unique ID so is filled in every data row.

    dim last_row as long
    ActiveSheet.Outline.Showlevels RowLevels:=2
    last_row = ActiveSheet.cells(rows.count,"A").end(xlup).row
    ActiveWorkbook.Worksheet("A-L").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("A-L").Sort.SortFields.Add Key:=Range("L2:L" & last_row), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("A-L").Sort.SortFields.Add Key:=Range("K2:K" & last_row), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("A-L").Sort.SortFields.Add Key:=Range("J2:J" & last_row), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("A-L").Sort.SortFields.Add Key:=Range("I2:I" & last_row), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("A-L").Sort.SortFields.Add Key:=Range("H2:H" & last_row), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("A-L").Sort
            .SetRange Range("A1:N" & last_row)
     '...
    If that's not enough, then probably my tool from the link above is not strong enough. And then I'll write basically the same as few posts above but in more open way:

    Will you please attach a SMALL sample Excel workbook(s) (10-20 rows of data is usually enough)? Please don't attach a picture of one (no-one will want to re-type all your stuff before starting).

    1. Make sure that your sample data are truly REPRESENTATIVE of your real data. (If trere are typical cases like: all unique values/duplicates could occur, day/night, nobody present/several persons at once, before/on/past due, etc. - please show them all or at least indicate in text) The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired solution(s) is/are also shown (mock up the results manually).

    3. Make sure that all confidential/restricted information (either personal or business) like real e-mails, social security numbers, bank accounts, etc. is removed first!!

    To attach an Excel file you have to do the following: Just before posting, scroll down and press Go Advanced button and then scroll down and press Manage Attachments link. Now follow the instructions at the top of that pop-up screen.
    Last edited by Kaper; 11-02-2018 at 04:00 AM.

  5. #5
    Registered User
    Join Date
    10-24-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    7

    Re: Sorting by Alpha, Adding Subtotal - then removing subtotal - variable data sets

    Sorry about the delay. I have now replicated the data in original form - and then the desired output.

    Couple of things to note;

    This macro will be run each month
    The number of rows will be different each month (could be less, could be more)
    The Macro will start on another tab/sheet
    It will need to run on sheet "A-L" & "M-Z"

    Thanks again! I'm sure the solution is simple
    Attached Files Attached Files

  6. #6
    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: Sorting by Alpha, Adding Subtotal - then removing subtotal - variable data sets

    Almost a month... At the moment I'm quite busy with other tasks, so anyone who is about to join the discussion - please do.

  7. #7
    Registered User
    Join Date
    10-24-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    7

    Re: Sorting by Alpha, Adding Subtotal - then removing subtotal - variable data sets

    Bump to see if anyone can assist - thanks

  8. #8
    Registered User
    Join Date
    10-24-2018
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    7

    Re: Sorting by Alpha, Adding Subtotal - then removing subtotal - variable data sets

    Can anyone help please?

+ 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. need subtotal to also include the matching data in subtotal line
    By baby_kay_2003 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-15-2014, 12:43 AM
  2. adding IF statement to subtotal for autofiltered data
    By archedjumpshot in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-21-2013, 12:33 PM
  3. Replies: 1
    Last Post: 09-18-2013, 04:10 PM
  4. Replies: 8
    Last Post: 07-31-2012, 09:41 AM
  5. Subtotal - Delete zero Subtotal and prior rows that calculate to that zero Subtotal
    By Whatsherface in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2012, 08:37 PM
  6. adding IF statement to subtotal for autofiltered data
    By fbarbie in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-07-2005, 03:00 PM
  7. [SOLVED] subtotal-isplaying data without using function subtotal ?
    By Marijan Glavaè in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-19-2005, 09:05 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