Results 1 to 9 of 9

[SOLVED] SUMPRODUCT with DYNAMIC range

Threaded View

  1. #8
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: SUMPRODUCT with DYNAMIC range

    Quote Originally Posted by hrlngrv View Post
    The last nonblank row in col B would be given by =MATCH(1,INDEX(0/NOT(ISBLANK(B:B)),0)).
    Quote Originally Posted by BMV View Post
    . . .
    - it's worse then use 100 000 in first formula because this operation will calculate 1 048 576 cells. and
    =LOOKUP(2;1/(B:B<>"");ROW(B:B))
    - much shorter quicker and with the same problem.
    In a new workbook, enter

    A1: 0
    B1: x
    A2: =CHOOSE(A1,MATCH(1,INDEX(0/NOT(ISBLANK(B:B)),0)),LOOKUP(1,0/(B:B<>""),ROW(B:B)),MATCH(1,INDEX(0/NOT(ISBLANK(prune(B:B))),0)),lastrow(B:B))

    Insert a general VBA module, and enter this code.

    Option Explicit
    
    
    Function prune(rng As Range) As Range
      Set prune = Intersect(rng, rng.Parent.UsedRange)
    End Function
    
    
    Function lastrow(rng As Range) As Long
      If rng.Columns.Count = 1 Then
        Set rng = Intersect(rng, rng.Parent.UsedRange)
        Set rng = rng.Rows(rng.Rows.Count)
        If IsEmpty(rng.Value2) Then Set rng = rng.End(xlUp)
        lastrow = rng.Row
      End If
    End Function
    
    
    Sub prof()
      Const MAXITER As Long = 200
      Const MAXBUMP As Long = 20
    
      Dim dt As Date, j As Long, k As Long
    
      For j = 1 To 4
        '# following 2 statements ensure a clean slate for each expression
        [B2:B1048576].ClearContents
        ThisWorkbook.Save
        dt = Now
        [A1].Value2 = j
        For k = 1 To MAXITER
          [B1].Offset([A2].Value2 + Application.WorksheetFunction.RandBetween(1, MAXBUMP), 0).Value2 = "x"
        Next k
        Debug.Print j, Format(dt, "hh:mm:ss"), Format(Now, "hh:mm:ss"), Format((Now - dt) * 86400# / MAXITER, "0.0000000000"), [A2].Value2
      Next j
    
      [A1].Value2 = 0
      [B2:B1048576].ClearContents
      ThisWorkbook.Save
    
    End Sub
    Now run prof. On my system (a VM with not a lot of RAM) I get the following results in the Immediate window.

    1 08:06:32 08:07:00 0.1399999982 2253
    2 08:07:01 08:07:24 0.1150000017 2193
    3 08:07:25 08:07:25 0.0000000000 2380
    4 08:07:26 08:07:26 0.0000000000 2386
    - -------- -------- ------------ ----
    3 08:07:50 08:08:00 0.0049999999 23279
    4 08:08:01 08:08:07 0.0030000001 22815

    3 and 4 each took less than a second for 200 iterations, so I increased MAXITER to 2000 and reran them. This does show that your lookup formula is about 18% faster than my match formula, but using a little VBA chops orders of magnitude off the calculation time. Note: <>"" isn't the same thing as blank. If the last cell with visible contents was B99 and there were IFERROR(...,"") formulas in B100:B200 all returning "", your lookup formula would return 99 while my match formula would return 200.
    Last edited by hrlngrv; 02-12-2020 at 12:32 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Sumproduct over dynamic range
    By sipa in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-29-2019, 08:20 AM
  2. Dynamic range from which to use =SUMPRODUCT()
    By Dralky in forum Excel General
    Replies: 10
    Last Post: 06-30-2018, 12:59 AM
  3. Replies: 2
    Last Post: 01-20-2017, 04:27 PM
  4. Sumproduct of Dynamic Range
    By deepak2jadav in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-28-2014, 09:01 AM
  5. Sumproduct for dynamic range
    By Bob@Sun in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-05-2010, 06:32 AM
  6. Sumproduct, dynamic range
    By jmicdk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-18-2008, 08:43 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