+ Reply to Thread
Results 1 to 6 of 6

Macro to subtract based on two criteria, and loop

  1. #1
    Registered User
    Join Date
    01-28-2017
    Location
    Miami, Florida
    MS-Off Ver
    2016
    Posts
    7

    Macro to subtract based on two criteria, and loop

    I have two sheets, sheet1 and sheet2.

    I have trying to come up with a macro that loops through each row in sheet1 and subtracts sheet1!I from sheet2!F if Sheet2!A2=Sheet1!E2 & Sheet2!D2=Sheet1!H2 until 0 and then find the next row in sheet2 where Sheet2!A2=Sheet1!E2 & Sheet2!D2=Sheet1!H2 until there is 0 remaining on sheet1.

    Hope the attachment helps.

    Note, some items on sheet1 will not correlate with anything on sheet2
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Macro to subtract based on two criteria, and loop

    See if the procedure below will do the trick, it seemed to work on your sample:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    01-28-2017
    Location
    Miami, Florida
    MS-Off Ver
    2016
    Posts
    7

    Re: Macro to subtract based on two criteria, and loop

    Thanks CAntosh,

    This seems to be working perfectly! I do have a few more questions though, if you would be so kind.

    Is there anyway we can set it so it is not case sensitive? I have a few instances where capitalization isn't allowing the locations or items to match correctly, or where one sheet will have a trailing space at the end and the other does not.

    I have to remove product description from Sheet1!H ; I currently use
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    in a cell and drag though, then copy and paste over the original Sheet1!H. (It removes everything after the space. So original output is 3856 (Plated Kitchen Sink) the formula gives me 3856, I copy all and past over.

    Could we have the results on sheet2!H instead of replacing sheet2!F , or create a copy of sheet2F! and paste into Sheet2!H prior to running the macro to subtract sales?

    How could I add this formula
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    into cell Sheet2!K2 and drag all the way through. (This is the sold quantity for a reference) -- not required.


    Is it possible to add a macro above this? I have another macro I run prior that is a mass find and replace so the locations match on each sheet. (Our output record(sheet1) uses different names for location than the order sheet))

    I would do this all manually, but I am afraid the end user would not be able to follow. (They are not very good with excel in general)

    Again, thank you for your support it is a great help!

  4. #4
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Macro to subtract based on two criteria, and loop

    Alright, new version:

    Please Login or Register  to view this content.
    Putting "Option Compare Text" at the start of the module should fix the case-sensitivity issue. The problem with leading/trailing spaces can best be solved by wrapping your earlier formulas in a "TRIM" function, e.g.:

    =TRIM(LEFT(H4,FIND(" ",H4&" ")-1))

    The new version should leave Sheet2, column F alone and create a new quantity total in column H. It should also add your SUMIFS formula to column K.

    I don't see any issue with this sub procedure following another, as long as everything in your workbook is in the right place before this procedure runs. You can either call UpdateQty from your first macro or you can paste my code within your other procedure, just be sure that the Option Compare Text is at the very top and make sure you don't have any overlapping definitions with the first part of the process.

    Give it a go, let me know if you have any issues...

  5. #5
    Registered User
    Join Date
    01-28-2017
    Location
    Miami, Florida
    MS-Off Ver
    2016
    Posts
    7

    Re: Macro to subtract based on two criteria, and loop

    Thanks again cAntosh,

    It appears to be working as hoped. I have integrated all the other features together and appears to work flawlessly.

    Last question if I remove
    Please Login or Register  to view this content.
    from the end I shouldn't expect any issues should I? Currently just using to illustrate how the macro is working without flipping back and forth between sheets.

  6. #6
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Macro to subtract based on two criteria, and loop

    Glad I could help! All that last line does is add the formula you requested in post #3 to column K of sheet 2. If you don't need the formula there (or prefer to do it without VBA), then removing the line won't affect anything else.

+ 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] Macro Loop: Creating multiple workbook based on autofilter criteria
    By Carlo_02 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-28-2016, 05:47 PM
  2. [SOLVED] For loop sum cells based on criteria.
    By Petter120 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2014, 09:04 AM
  3. Formula to subtract Financial Quarters based on different criteria.
    By arvimeld in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-03-2014, 10:33 AM
  4. [SOLVED] VBA Loop: Insert Row based on Criteria
    By Ada01 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-24-2013, 07:14 PM
  5. Macro to subtract 2 cells based on the Value
    By excel199 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-14-2010, 05:36 AM
  6. SUMIF based on certain criteria - and then subtract neg. values
    By Darlo in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-20-2009, 10:09 AM
  7. Advancing outer Loop Based on criteria of inner loop
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-15-2005, 01:05 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