Vbalearner85
Board Regular
- Joined
- Jun 9, 2019
- Messages
- 139
- Office Version
- 2016
- Platform
- Windows
I have a simple VBA code as below to copy some formulas and then do some more steps. This throws vba memory error either on ".value" or ".numberformat" steps. Memory usage/CPU usage suddenly jumps like 90%. Not sure if I am doing something fundamentally wrong. Using 2016 Excel version on windows 10. Please advise
I then run this code in a loop on a large file(300MB) to format around 150+ sheets.
I then run this code in a loop on a large file(300MB) to format around 150+ sheets.
VBA Code:
Sub Format()
Dim lr As Long, lastrow As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "BH").End(xlUp).Row
Range("BR1").Value = "MP"
Range("BS1").Value = "PEP"
Range("BR2").Formula = "=IF(AND(OR(AY2=""LP"",AY2=""HP""),BA2>$BZ$1),BH2-BF2,"" "")"
If lr > 2 Then Range("BR2").AutoFill Destination:=Range("BR2:BR" & lr)
Range("BS2").Formula = "=IF(AND(OR(AY2=""LP"",AY2=""HP""),BA2>$BZ$1),BE2-BF2,"" "")"
If lr > 2 Then Range("BS2").AutoFill Destination:=Range("BS2:BS" & lr)
ActiveSheet.Calculate
Columns("BR").Value = Columns("BR").Value
Columns("BS").Value = Columns("BS").Value
Intersect(Range("2:" & lr), Range("BR:BS")).NumberFormat = "##,##0.0;[Red](##,##0.0)"
Columns("BR:BS").EntireColumn.AutoFit
Application.ScreenUpdating = True
End Sub