I want to hide some of the Rows in a worksheet if the Cell in column A contains 0. This is calculated using a formula.
E.g. If A1 contains value "0", row#1 should get hidden.
I know I can write this code on Sheet's Activate Event but it is slowing down the file's performance.
Is there a good way to achieve the same without compromising file's performance?
you could always use filter to achieve this. I think that would be much more efficient.
Edit:
To improve performance of your code, you could do the following:
Turn off the Events
Turn off the Calculations
Before running the loop to hide rows
With Excel.Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
.Cursor = xlWait
End With
After the loop is over
With Excel.Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.Cursor = xlDefault
End With
Hope this helps. Also explicitly using variables could give a boost to performance
and Calculation. Although explicitly declaring your variables should help too.
Related
I have a complex Excel with lot of tables and formulas.
I stop and resume calculations via VBA code:
Turn OFF calculations:
ActiveSheet.EnableCalculation = False
Application.Calculation = xlCalculationManual
Turn ON calculations:
ActiveSheet.EnableCalculation = True
Application.Calculation = xlCalculationAutomatic
Application.Calculate 'This line calculates all open books and is not strictly necessary.
But still the cells are not calculated.
They are only calculated when I edit each cell with double click and hit Enter (one by one!).
The code doesn't give me any errors and works perfectly on simple books.
The problem is "EnableCalculation" method.
If I remove it, then the book works perfect. So simply toggle ON/OFF calculations with xlAutomatic/xlManual, like this:
Turn OFF calculations:
Application.Calculation = xlCalculationManual
Turn ON calculations:
Application.Calculation = xlCalculationAutomatic
I used to have this formula below copied into multiple cells, but at some point it wasn't pulling the data properly so I had to modify it to the formula below it. The SUMPRODUCT formula finds data from another spreadsheet based off the part number and pulls it over to the new spreadsheet.
=IFERROR(VLOOKUP($B14,'G:\Locations_NA\TUS\LO\[DMSU MACRO DATA.xlsm]DDCPIV'!$A:$W,13,FALSE),0)
=IFERROR(SUMPRODUCT(('G:\Locations_NA\TUS\LO\[DMSU MACRO DATA.xlsm]DDC SD'!$F$4:$AT$10000)*('G:\Locations_NA\TUS\LO\[DMSU MACRO DATA.xlsm]DDC SD'!$A$4:$A$10000=$B14)*('G:\Locations_NA\TUS\LO\[DMSU MACRO DATA.xlsm]DDC SD'!$F$3:$AT$3=$AD$13)),0)
I run a macro that someone else made which, besides doing other things, fills down this formula a couple hundred rows. Now when I run it with the new formula it takes ages compared to how long it used to take. Is there a better way I can go about this to speed it up?
I would recommend these three basic things, that will help you without modifying much your code.
First: Change VLOOKUP or SUMPRODUCT to "XLOOKUP". This is a faster new formula
it will be something like this
=IFERROR(XLOOKUP(R14C12, _
'G:\Locations_NA\TUS\LO\[DMSU MACRO DATA.xlsm]DDCPIV'!$A:$A, _
'G:\Locations_NA\TUS\LO\[DMSU MACRO DATA.xlsm]DDCPIV'!$W:$W,_
"Error",0),0)
Second: Try to set the formula by a bunch of cells, not one by one (I'm assuming this, if you share more of your code we can check.
one by one is:
Range("A1").FormulaR1C1 =IFERROR(XLOOKUP(R14C12 ...
Range("A2").FormulaR1C1 =IFERROR(XLOOKUP(R15C12 ...
Bunch of cell is:
Range("A1:A5").FormulaR1C1 =IFERROR(XLOOKUP(R14C12 ...
Third: as ENIAC just said, disable UpdatingScreen and other Automatic Updates that Excel has. This will be like this:
Sub Example()
'Beginning Code
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
.DisplayAlerts = False
End With
'Your code Here
'End Code
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.DisplayAlerts = True
End With
End sub
Now, the best way, in my opinion, is to work with Arrays and Dictionaries, that system is much faster, very much.
I have two workbooks, one is for work (A) and the other only has usage data (B). File B has more than 25000 records and when I open it using VBA there is a long delay in loading.
One option I thought of was to delete the sheets with the data that I don't need but should delete them without opening the B file.
The second option would be to copy the data from the sheet without opening file B.
Currently, I am using Set wbOrigen = Workbooks.Open (FileName: = xxxxxx) but I need to speed up the load.
Is it possible to do any of this?
As far as I know it is not possible to access a Workbook's data without opening it. But from my experience this is not a problem (as this is very fast) as long as you remember to close it in the end.
The "standard" trick to improve performance is to disable ScreenUpdating and Events like such:
Application.ScreenUpdating = False
Application.EnableEvents = False
' Open Workbook, Load data, do some operations, close Workbook
Application.ScreenUpdating = True
Application.EnableEvents = True
Thanks but I already using those declarations, that is not the problem.
Public Sub TLD_StartMacro()
With Application
.ScreenUpdating = False
.Calculation = Excel.xlCalculationManual
.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
.CutCopyMode = False
.DisplayAlerts = False
End With
End Sub
Public Sub TLD_EndMacro()
With Application
.DisplayAlerts = True
.ScreenUpdating = True
.Calculation = Excel.xlAutomatic
.EnableEvents = True
.CutCopyMode = False ' Esta sentencia vacĂa el portapapeles
End With
End Sub
I have a very simple VBA code running in workbook A that is rather slow when another specific workbook B is open. I don't have access to any macros inside workbook B, so I can't know for sure if anything wierd is happening there.
I've looked around and came with this minimal code condensing the typical solutions I've found in stackoverflow and many Excel specific sites, but it still takes a long time to run.
' Code in workbook A
Sub slow_simple_macro()
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
.DisplayStatusBar = False
End with
Workbooks("workbookA.xlsm").Sheets("Sheet1").Range("A1") = "Slow" ' This line takes about half a second to run when workbook B is open
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
.DisplayStatusBar = True
End with
End Sub
I'm guessing that disabling events is not enough to prevent something inside workbook B from running. Are there any other Application flags I can setup to make it run faster? Any other ideas?
You can see while disabling some functions of excel like calculation mode, screenupdates,status bar would impact on application of Excel because "Excel is the one of The object model that is a large hierarchy of all the objects used in VBA" (Source:www.globaliconnect.com).
So if you want run by turn off application functions , you should be wait till you set back to its original or else same problem would happen.
Better simplify code as possible unless if its required
Sub slow_simple_macro()
Workbooks("workbookA.xlsm").Sheets("Sheet1").Range("A1") = "Slow"
End Sub
Hope you find it as helpful. Have a good day.
I have a COM (C++) API that listens for data updates from a server and writes these updates to a sheet. These updates are handled in VBA code and can arrive multiple times a second. In order to write these updates to the sheet in the most efficient manner, I use the following premise:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
<UPDATE CODE>
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
In fact, I schedule a procedure to do this regular intervals where ScreenUpdating = False for about 20 seconds, then it is set to true so the data can update and then I set it to false again. i have found that this is a better option than setting ScreenUpdates + Calculation explicitly simply because of the highfrequency of the updates I receive.
The Problem:
I have read here that excel sets ScreenUpdating = True at the end of each method that disables it which is not what I need.
The Question:
Is there some way to force Excel to not automatically enable ScreenUpdating?
See if these help with your APIs
http://msdn.microsoft.com/en-us/library/ff818516%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ee461765%28v=vs.85%29.aspx
I just searched google for "list of apis microsoft"