+ Reply to Thread
Results 1 to 4 of 4

Sumproduct with Greater Than Less Than

  1. #1
    Registered User
    Join Date
    11-13-2006
    Posts
    16

    Sumproduct with Greater Than Less Than

    Hi All,

    In column K, I am calculating a Sumproduct based on the criteria in columns C, D, & G. The data in all three should match. Column I has the numeric data. If column C, D, & G match and the cumulative value in Column I is greater than $1000 or less than $-1000 then I would like to return "Out of Tolerance", otherwise, "" (leave cell blank).

    The code below works but takes an extraordinary amount of time to run because of the massive amounts of data I need. Can you help? Thanks for your time

    Dim myLastRowA As Long
    myLastRowA = Range("A65536").End(xlUp).Row

    Range("k2").Select
    ActiveCell.FormulaR1C1 = "=IF(SUMPRODUCT(--(R1C3:R65000C3=RC[-8]),--(R1C4:R65000C4=RC[-7]),--(R1C7:R65000C7=RC[-4]),R1C9:R65000C9)>=1000,""Out of Tolerance"",IF(SUMPRODUCT(--(R1C3:R65000C3=RC[-8]),--(R1C4:R65000C4=RC[-7]),--(R1C7:R65000C7=RC[-4]),R1C9:R65000C9)<=-1000,""Top"",""""))"

    Range("k2").AutoFill Destination:=Range("k2:k" & myLastRowA)
    Range("k2:k" & myLastRowA).Select

  2. #2
    Forum Contributor
    Join Date
    07-05-2006
    Location
    Canada
    MS-Off Ver
    2003, 2007
    Posts
    581
    I do have questions about your implementation. Why are you doing the SUMPRODUCT twice? Could you not go =IF(ABS(SUMPRODUCT(..))>1000, ... , ...)? (That might half the time -- not sure how MS implemented things when you use the same formula twice)

    Second, for column K, you're finding the number of rows, but for columns C,D,G you're saying the limit is 65,000. (Seems to me there is the potential for missed data there)

    My suggestion:

    Basically, it's not to use SUMPRODUCT. You're repeating multiple calculations, so you can do it smarter than SUMPRODUCT can because it doesn't know this.

    1) Insert a column somewhere. (Probably on the far end of your data)

    2) Number it from 1 to the number of rows you want to look at. The purpose of this is to return to the original ordering, because part of my suggestion involves sorting.

    3) Sort the data by Column C, D, G. What does this do? It makes it so all rows that pertain to the same values in C, D, G are contiguous. The benefit? It means we don't need to look at all 65,000 rows to find the data.

    4) Using code similar to the following, go through each row to determine whether it is Out of Tolerance.
    Please Login or Register  to view this content.
    5) Re-sort your data by the column that was inserted with the line numbers.

    6) Delete the extra column.

    If you don't care about the order of the data, skip steps 1, 2, 5, and 6.

    This won't be ultra-fast... but it will be significantly faster than what you had. If you want it to be faster, you'll have to pull the 4 columns into memory initially. The do your calculations and output the results to another array. Then dump that array back into the worksheet. However, this method above is probably simpler to code. If you find the speed to be still too slow, say so.

    Scott

  3. #3
    Registered User
    Join Date
    11-13-2006
    Posts
    16
    Hi Scott,

    Good Advice.

    I can't get past this... when i run the macro it errors and says that an object is required for the following... I'm still new to VBA... Your help is much appreciated.

    t = WS.Cells(i, 9) 'Column I

  4. #4
    Forum Contributor
    Join Date
    07-05-2006
    Location
    Canada
    MS-Off Ver
    2003, 2007
    Posts
    581
    Unfortunately, I probably need to see all your code to help.

    The only possibility that I can think of off the top of my head is that WS isn't declared, but it should be declared in the parameters to the subroutine.

    ie. Public Sub CalculateIt(i As Long, WS As Worksheet)

    Scott

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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