Hi all
I have a large spreadsheet I received from a client (many tabs, Vlookup type formulas etc.). I am running a macro over it for analytical purposes which does not require the spreadsheet to recalculate (it is effectively extracting information from a sheet and copies it to another sheet in the same workbook). I have included the code below to see what performance improvements could be made. I add two lines to the macro to improve performance but it doesn't seem to make much difference:
Application.Calculation = xlManual
Application.ScreenUpdating = False
I have also noticed that manually copying and pasting (value) a single cell from one worksheet to another carries the minutest delay. It is hence possible that the spreadsheet is somewhat overburdened.
Full code below
I have a large spreadsheet I received from a client (many tabs, Vlookup type formulas etc.). I am running a macro over it for analytical purposes which does not require the spreadsheet to recalculate (it is effectively extracting information from a sheet and copies it to another sheet in the same workbook). I have included the code below to see what performance improvements could be made. I add two lines to the macro to improve performance but it doesn't seem to make much difference:
Application.Calculation = xlManual
Application.ScreenUpdating = False
I have also noticed that manually copying and pasting (value) a single cell from one worksheet to another carries the minutest delay. It is hence possible that the spreadsheet is somewhat overburdened.
Full code below
VBA Code:
Sub DataModel()
'***
'* Last update: 21 October 2021
'*
'***
Dim wb As Workbook
Dim wsSource As Worksheet, wsTarget As Worksheet, wsValue As Worksheet
Dim rngSource As Range
Dim n As Integer, m As Integer, i As Integer, j As Integer, cnt As Integer
Set wb = ThisWorkbook
With wb
Set wsSource = .Sheets("Production volumes ")
Set wsTarget = .Sheets("Target")
'Set wsValue = .Sheets("Values")
End With
'Application settings
Application.Calculation = xlManual
Application.ScreenUpdating = False
cnt = 1
With wsSource
Set rngSource = .Range("A3:H23")
n = rngSource.Rows.Count
m = rngSource.Columns.Count
For i = 2 To n
For j = 2 To m
If rngSource(i, j) <> "" Then
cnt = cnt + 1
wsTarget.Cells(cnt, 1) = rngSource(1, j) 'Country/company
wsTarget.Cells(cnt, 2) = rngSource(i, 1) 'Account
wsTarget.Cells(cnt, 3).FormulaR1C1 = _
"=""'""&RC[-2]&""' --> '""&RC[-1]&""'""" 'Point-of-View
wsTarget.Cells(cnt, 4).FormulaR1C1 = _
"=IFERROR(INDEX(Values!R20C2:R777C2,MATCH(Target!RC[-2],Values!R20C3:R777C3,0),1),"""")" 'Cognos Account
wsTarget.Cells(cnt, 5) = Chr(39) & Chr(61) & rngSource(i, j).Parent.Name & "!" & rngSource(i, j).Address(RowAbsolute:=True, _
ColumnAbsolute:=True) 'Cell Reference
wsTarget.Cells(cnt, 6) = Chr(39) & rngSource(i, j).Formula 'Excel Formula
End If
Next
Next
End With
'Do a secondary loop to replace Excel cell reference in Excel Formula with Point-of-View
'Application settings
Application.Calculation = xlAutomatic
Application.ScreenUpdating = False
End Sub