is VBA this slow?

henrik2h

Board Regular
Joined
Aug 25, 2008
Messages
155
Office Version
  1. 2021
Platform
  1. Windows
OK, if you have seen this macro in another similar question recently, I am a clown and completely messed up my timer so I looked for problem in the wrong place...

I have this macro that place input (number 1 to 100) in a cell and then copy and store the resulting range of outcome in another sheet. It takes 60-70 seconds for 100 inputs (I would like many more).

Just made a macro that does the same, loop through 1 to 100 but I never copy the outcome, it just goes on to the next input (not the one below). That takes 2 seconds!

When it comes to the formulas in the outcome range I have streamlined them in every possible way, there is 'nothing' that can be done further.

Is this the reality of VBA/copying from workbook to VBA, or is it something that I completely mess up?

Code:
Sub Calc_to_pivot()

' Calculate all leases and copy to Pivot sheet


    Application.ScreenUpdating = False
    Application.Iteration = False
    
    Dim i As Long, j As Long, Lease_Count As Long, PivotSource As Range, k As Integer, Starttime As Double, TimeElapsed As Double
    Dim arrS As Variant, arrT As Variant, L As Long, Row As Long, istart As Long, msg As String
    
    Set PivotSource = ActiveWorkbook.Sheets("Flow_calculation").Range("AC208:BT251")
    k = 44  ' number of columns (transposed to rows) to offset
    
    Lease_Count = Sheets("INP_PREM").ListObjects("tbRentRoll").DataBodyRange.Rows.Count
    
    ReDim arrS(1 To k, 1 To 44)
    ReDim arrT(1 To Lease_Count * k, 1 To 44)
    
    Sheets("Pivot").Range("A161:AR1000000").ClearContents
    ActiveSheet.DisplayPageBreaks = False
            
     ' turn off automatic calculation except for "Flow_calculation"
     Application.Calculation = xlCalculationManual
     
     With Sheets("Flow_calculation")
     Application.Calculation = xlCalculationAutomatic
     End With
        
    Sheets("Flow_calculation").Activate

===SLOW, recalculating in Excel 100 times takes 2 sec, in macro 60 sec=== 
    'Calculate for all leases, one by one
    For L = 1 To Lease_Count
    
     'Choose lease from row L
    ActiveSheet.Range("B3").Value = L
    
    ' tranpose range and put in array
    arrS = Application.Transpose(PivotSource.Value2)
    
===Fast, no problem===
    'check for errors
    If ActiveSheet.Range("B207").Value = "ERROR" Then
    msg = "Error in lease row " & L
    MsgBox msg, vbOKOnly, "ERROR"
    Sheets("Pivot").Range("A161:AR1000000").ClearContents
    Sheets("CASH_FLOW").Select
    Range("D31").Select
    Sheets("Lease_analysis").Select
    Range("B9").Select
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Exit Sub
    End If
===    
===Fast, no problem===    
    istart = (L - 1) * k
    
        For i = 1 To k
        Row = istart + i
            For j = 1 To 44
            arrT(Row, j) = arrS(i, j)
            Next j
        Next i
===
        
    Next L
    
    
===Fast, no problem===    
    ' Paste array of value onto sheet Pivot
    Set Destination = Sheets("Pivot").Range("A161")
    Destination.Resize(UBound(arrT, 1), UBound(arrT, 2)).Value = arrT
    Application.Calculation = xlCalculationAutomatic
===    
    ' resize pivot source data and refresh
    
===ignore this, it is only a few seconds at the end====
Call AdjustPivotDataRange
===    
    Application.ScreenUpdating = True






End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top