Newbie here.
I'm trying to delete a column using vba and it is taking a HUGE amount of time.
Sub Add_Delete()
Application.ScreenUpdating = False
Workbooks("f.xlsx").Sheets("g").Columns("FD:FD").EntireColumn.Delete
End Sub
There are about 20,000 rows with a total of amount 230 columns. There are no calculations being performed. Just data
What am I doing wrong? Thanks in advance!
If it is slow manually also turn off calculation before running it, then turn it back on.
Sub Add_Delete()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Workbooks("f.xlsx").Sheets("g").Columns("FD:FD").EntireColumn.Delete
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
add screen updating back in also... ;-)
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'm trying to create a hotkey that will toggle Application.ScreenUpdating. I just learned that the newer Excel versions automatically reset Application.ScreenUpdating to true after running a macro which is good 99.99% of the time except I don't want that to happen I want it to be persistent. Is there anyway for me to keep Application.ScreenUpdating = False?
Sub suspend_screen_update()
MsgBox "1st " & Application.ScreenUpdating
If Application.ScreenUpdating = True Then
Application.ScreenUpdating = False
Else
Application.ScreenUpdating = True
End If
MsgBox "2nd " & Application.ScreenUpdating
End Sub
Private Sub hk_toggle_screen_update()
Application.OnKey "{F1}", suspend_screen_update
End Sub
Why? My boss sometimes request that I only use Excel, nothing else, to manipulate a lot of data. Excel can be super slow if not crash when the data gets really big. I'm trying to save us time by creating this hotkey.
I am developing an add in for excel using vba. Upon opening the spreadsheet, all the values are recalculated and this slows down the opening. I've tried creating a Workbook_Open event that changes the application calculation from automatic to manual, but this event handler is executed after the calculations are done. I also tried setting the calculation to manual before the spreadsheet closes, so that upon opening it next time it will be faster. My problem with this though is that I feel it is invasive to the client.
Ideally what I would like to do is:
When the spreadsheet opens get the user's current calculation setting and save it
Change the calculation setting to manual so that the spreadsheet can open quickly
Change the calculation setting back to the user's original setting
How can I go about doing this?
I created 2 subs that I call when I need to do this, Updates_Off and Updates_On. You will need to declare the variables CalcMode, IterationMode, and Iterations as public in your calling sub and this will only work if you turn updates back on before you exit. If you can't do that, you will need to have some helper cells that store the values.
I have the if CalcMode=xlCalculationManual statements to warn me during development that I'm starting or ending in manual mode and I uncomment them when needed.
Public Sub Updates_Off()
' Turn off Screen updating
Application.ScreenUpdating = False
' Check what calculation mode is in effect and set current to manual
CalcMode = Application.Calculation
' If CalcMode = xlCalculationManual Then _
' MsgBox "Starting mode is manual"
IterationMode = Application.Iteration
Iterations = Application.MaxIterations
Application.Calculation = xlCalculationManual
End Sub
Public Sub Updates_On()
' Turn on Screen updating
Application.ScreenUpdating = True
' Reset Calculation mode
Application.Calculation = CalcMode
Application.Iteration = IterationMode
Application.MaxIterations = Iterations
' If CalcMode = xlCalculationManual Then _
' MsgBox "Reset to manual mode"
End Sub
I am using a worksheet change function to give my excel spread sheet the illusion of a search bar with a drop down box containing the results of the text in the search bar.
before I just had the hide rows part of my code which would hide and then unhide some rows in my spread sheet containing the results. that worked fine but the results would sometimes be slow and not always show up until I re calculated them.
so I added calculate to the ranges and this unfortunately slows the whole thing down substantially. Is there a better way to do this?
Private Sub Worksheet_Change(ByVal target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
If Range("D11").Value <> "" Then
Dim xlpassword As String
xlpassword = "Perry2012"
ActiveSheet.Unprotect xlpassword
ActiveSheet.EnableCalculation = False
ActiveSheet.EnableCalculation = True
Worksheets("HOME").Range("A1").Calculate
Worksheets("HOME").Range("D33").Calculate
Worksheets("HOME").Range("D32").Calculate
Worksheets("HOME").Range("E33").Calculate
Worksheets("HOME").Range("E32").Calculate
Worksheets("HOME").Range("F33").Calculate
Worksheets("HOME").Range("F32").Calculate
Worksheets("HOME").Range("G33").Calculate
Worksheets("HOME").Range("G32").Calculate
Worksheets("HOME").Range("H33").Calculate
Worksheets("HOME").Range("H32").Calculate
Worksheets("HOME").Range("I33").Calculate
Worksheets("HOME").Range("I32").Calculate
Worksheets("HOME").Range("J33").Calculate
Worksheets("HOME").Range("J32").Calculate
Worksheets("HOME").Range("K33").Calculate
Worksheets("HOME").Range("K32").Calculate
Worksheets("HOME").Range("L33").Calculate
Worksheets("HOME").Range("L32").Calculate
Worksheets("HOME").Range("M33").Calculate
Worksheets("HOME").Range("M32").Calculate
Worksheets("HOME").Range("N33").Calculate
Worksheets("HOME").Range("N32").Calculate
Worksheets("HOME").Range("O33").Calculate
Worksheets("HOME").Range("O32").Calculate
Worksheets("HOME").Range("P33").Calculate
Worksheets("HOME").Range("P32").Calculate
Worksheets("HOME").Range("Q33").Calculate
Worksheets("HOME").Range("Q32").Calculate
Worksheets("HOME").Range("R33").Calculate
Worksheets("HOME").Range("R32").Calculate
Worksheets("HOME").Range("S33").Calculate
Worksheets("HOME").Range("S32").Calculate
Worksheets("HOME").Range("T33").Calculate
Worksheets("HOME").Range("T32").Calculate
Worksheets("HOME").Range("U33").Calculate
Worksheets("HOME").Range("U32").Calculate
Worksheets("HOME").Range("V33").Calculate
Worksheets("HOME").Range("V32").Calculate
Worksheets("HOME").Range("W33").Calculate
Worksheets("HOME").Range("W32").Calculate
Worksheets("HOME").Range("D15").Calculate
Worksheets("HOME").Range("D17").Calculate
Worksheets("HOME").Range("D19").Calculate
Worksheets("HOME").Range("D21").Calculate
Worksheets("HOME").Range("D23").Calculate
Worksheets("HOME").Range("D25").Calculate
Worksheets("HOME").Range("D27").Calculate
Worksheets("HOME").Range("M15").Calculate
Worksheets("HOME").Range("M17").Calculate
Worksheets("HOME").Range("M19").Calculate
Worksheets("HOME").Range("M21").Calculate
Worksheets("HOME").Range("M23").Calculate
Worksheets("HOME").Range("M25").Calculate
Worksheets("HOME").Range("M27").Calculate
Worksheets("HOME").Range("T15").Calculate
Worksheets("HOME").Range("T17").Calculate
Worksheets("HOME").Range("T19").Calculate
Worksheets("HOME").Range("T21").Calculate
Worksheets("HOME").Range("T23").Calculate
Worksheets("HOME").Range("T25").Calculate
Worksheets("HOME").Range("T27").Calculate
Rows("15:28").Hidden = False
Rows("34:36").Hidden = True
Else
Rows("15:28").Hidden = True
Rows("34:36").Hidden = False
ActiveSheet.Protect xlpassword
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Would replacing all those separate lines by just one work:
Application.Calculate
it might be the case that you have some volatile functions heavily used in the workbook(i.e. TODAY()), which I reckon will make all formulas based on them re-calculate on each of those calculation lines in your code
I use huge excel worksheets and they take forever to load because of cell calculations using macros and other formulas (over 5 minutes even if I have a good computer) . I was wondering if there was a way to save the excel files with the current cell values instead of calculating the cells each time I open the file.
What I am looking for is like a switch that would turn the calculations on and off so that when I need to use them I could set them to on, and when I am done, I could switch it to off and the cells would keep their current values.
Maybe I could create a macro that would do something like that, or maybe I am just dreaming and there is no other way around, so I should just sit and wait.
We have a similar problem.
This is what we use:
Function TurnOfCalcs()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.DisplayAlerts = False
End Function
We turn off calculations, screenupdating, alerts and events while the initial data is loading and updating.
Once the streaming data from the sheet has finished we turn updates back on like so:
Function TurnOnCalcs()
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.DisplayAlerts = True
End Function
You still have the udpate time but this means you don't' do updates after each single cell change, which should dramatically speed up your file loading times.
You can set the calculation options for Excel both via the GUI and via VBA: Application.Calculation = xlManual
Here is some more info... http://www.decisionmodels.com/calcsecretse.htm
Use:
Private Sub Workbook_Open()
Module1.TurnOff
End Sub
Then have a button than is assigned this macro
Sub Button1_Click()
If Application.EnableEvents = True Then
TurnOff
Else
TurnOn
End If
End Sub
Then have this in a module:
Public Sub TurnOn()
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
Public Sub TurnOff()
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub