+ Reply to Thread
Results 1 to 9 of 9

Macro to apply filter, use if statement to generate subtotal, return value

  1. #1
    Registered User
    Join Date
    05-12-2017
    Location
    Canada
    MS-Off Ver
    Mac - 15.27
    Posts
    12

    Macro to apply filter, use if statement to generate subtotal, return value

    Hello,

    My workbook has two sheets: Input (large bank of data) and Output (simplified presentation sheet)

    I need a macro that will perform in the following order:

    (1)sort the Input sheet by column A
    (2)On the Input sheet run an if statement on column K and L where --> If (absolute(Column K)) - (Absolute(Column L)) > 100, then the value of ($K - $L) is added to a running subtotal
    (3)return value of the summed subtotal to Output sheet


    My main problem comes with form the If statement to run a subtotal.

    Many thanks in advance Excel Forum.
    Last edited by VBA_Newguy; 05-18-2017 at 10:46 AM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Macro to apply filter, use if statement to generate subtotal, return value

    Try something like this. The total is put in sheet Output range A1.
    Assumed there is a header in row 1 of sheet Input.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    05-12-2017
    Location
    Canada
    MS-Off Ver
    Mac - 15.27
    Posts
    12

    Re: Macro to apply filter, use if statement to generate subtotal, return value

    Hi AlphaFrog,

    that sub seems to be having a problem when it comes to returning the value to the Output page when i run it.

    although your code did help me catch something that i should have mentioned:
    Rather than send the entire formula to the output page (Cell:A1), i only want the subtotal value to be sent to A1.

    i assume that means it should be stored as a variable somewhere?

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Macro to apply filter, use if statement to generate subtotal, return value

    Quote Originally Posted by VBA_Newguy View Post
    Hi AlphaFrog,

    that sub seems to be having a problem when it comes to returning the value to the Output page when i run it.

    although your code did help me catch something that i should have mentioned:
    Rather than send the entire formula to the output page (Cell:A1), i only want the subtotal value to be sent to A1.

    i assume that means it should be stored as a variable somewhere?
    Can you attach an example workbook? Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  5. #5
    Registered User
    Join Date
    05-12-2017
    Location
    Canada
    MS-Off Ver
    Mac - 15.27
    Posts
    12

    Re: Macro to apply filter, use if statement to generate subtotal, return value

    Hi AlphaFrog,

    I ended up scrapping that idea all together.
    However, i do need help with this If Statement

    my formula reads as followS:
    If (Abs(.Range("L2:L" & LastCell).Value) - Abs(.Range("K2:K" & LastCell).Value)) > 100 Then
    .Range("P2:P" & LastCell).Value = (.Range("L2:P" & LastCell).Value) - (.Range("K2:K" & LastCell).Value)

    im trying to take the absolute difference of rwo ranges ("K2:K" & Lastcell) and ("L2:2" & Lastcell) and, if that absolute difference is > 100, print the value in column P.

  6. #6
    Registered User
    Join Date
    05-12-2017
    Location
    Canada
    MS-Off Ver
    Mac - 15.27
    Posts
    12

    Re: Macro to apply filter, use if statement to generate subtotal, return value

    Update:

    Ive changed the If statement to the following:

    If Abs(.Range("L" & LastCell).Value - Abs(.Range("K" & LastCell).Value)) > 100 Then
    .Range("P" & LastCell).Value = Abs(.Range("L" & LastCell).Value - Abs(.Range("K" & LastCell).Value))

    the sub runs, but no values appear in column "P", any input anyone has would be appreciated

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Macro to apply filter, use if statement to generate subtotal, return value

    Not sure how you are defining LastCell.

    I think this is what you want for absolute difference.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    05-12-2017
    Location
    Canada
    MS-Off Ver
    Mac - 15.27
    Posts
    12

    Re: Macro to apply filter, use if statement to generate subtotal, return value

    LastCell is defined as:

    LastCell = Sheets(OOTab).Range("K1").End(xlDown).Row


    ill give that a try and report back, thank you.



    Edit:

    The sub still runs, but, once again, the values in column P are not populating.
    Strange.
    Last edited by VBA_Newguy; 05-19-2017 at 10:36 AM.

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Macro to apply filter, use if statement to generate subtotal, return value

    That's all I got based on the limited information provided.

    If you want to attach a sample workbook, I'll take a look. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

+ 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. Apply multiple filters based on custom filter form [macro?]
    By Speshul in forum Access Tables & Databases
    Replies: 1
    Last Post: 12-05-2016, 05:25 PM
  2. Macro: Filter spreadsheet, then apply formula to results
    By RC-AMG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-26-2015, 08:14 AM
  3. [SOLVED] showing the subtotal after clicking on the filter macro
    By extremis in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-30-2013, 04:11 PM
  4. VBA/Macro to apply a filter to a pivot chart
    By ianh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-01-2011, 06:04 AM
  5. Macro to apply text to filter
    By nods in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2010, 03:55 PM
  6. Apply filter then subtotal
    By staples in forum Excel General
    Replies: 5
    Last Post: 10-13-2009, 01:54 PM
  7. Subtotal Macro how can I apply #rounding?
    By Casey in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-04-2005, 02:08 PM

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