Results 1 to 8 of 8

Optimise VBA Code for SUMPRODUCT analysis

Threaded View

  1. #1
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,454

    Optimise VBA Code for SUMPRODUCT analysis

    I would welcome any thoughts, ideas or suggestions as to how I can optimise the VBA code listed below.

    This is running on Windows XP/Excel 2003 on a Toshiba Portege laptop with 512 Mb RAM

    I am trying to analyse the contents of a worksheet. To that end, I have created a matrix on another sheet. This matrix is 117 columns wide by 232 rows deep and is/was populated by SUMPRODUCT formulae. The SUMPRODUCT formula works and gives the correct results but the sheer volume made the worksheet/workbook unusable.

    To overcome the impact on the rest of the workbook, I had copied the formulae and pasted the values back. I now want to revisit the calculation and add another criteria.

    Initially, I just edited the formula and copied and pasted it to the rest of the matrix ... but the workbook appeared to hang. I decided then to use VBA to insert the formulae a column at a time and copy and paste the values. However, this process is taking around 7 to 9 seconds per column. What is worse, if I just let it run, it seems to hang.

    I can nurse the code through and it works but I could do without sitting for 15 minutes or so every time I run it. I have other sheets I will want to do similar analyses with so it's not just idle interest.

    Sub Analyse_AR()
    
    Dim calcState
    Dim lLRow As Long
    Dim lLCol As Long
    Dim lLC As Long
    
    If ActiveSheet.Name <> "Analysis (ar)" Then
        MsgBox "Select Analysis (ar) Sheet"
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    calcState = Application.Calculation
    Application.Calculation = xlCalculationManual
    
    lLRow = Range("B" & Rows.Count).End(xlUp).Row - 1
    lLCol = Cells(1, Columns.Count).End(xlToLeft).Column - 1
        
    For lLC = 2 To lLCol
        Debug.Print lLC; "  start "; Now()
        With Range(Cells(3, lLC), Cells(lLRow, lLC))
            .FormulaR1C1 = _
                "=SUMPRODUCT(('No 1 '!R2C7:R6600C7=RC1)*('No 1 '!R2C3:R6600C3=R1C)*('No 1 '!R2C11:R6600C11=""ar""))"
            Application.Calculate
            .Copy
            .PasteSpecial _
                Paste:=xlPasteValues
            Application.CutCopyMode = False
        End With
        Debug.Print lLC; "  end "; Now()
    Next 'lLC
    
    Application.Calculation = calcState
    Application.ScreenUpdating = True
    
    End Sub

    Thanks in advance for any help or guidance you can offer.

    Regards
    Last edited by TMS; 11-30-2010 at 11:52 AM.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


Thread Information

Users Browsing this Thread

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

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