Cutting a column and then inserting with excel vba - vba

Columns("DV").Cut
Columns("P").Insert Shift:=xlToRight
I am using this code above to basically move a column, but every time I run the macro, it slows the process so much. Is there a more efficient way to do this so my macro isn't bogged down?
Thanks.

You might try inserting this line at the beginning of the macro:
Application.Calculation = xlCalculationManual
And insert this at the end:
Application.Calculation = xlCalculationAutomatic
If you have a lot of formulas in the data you are moving around it is attempting to recalculate while the macro is running. The above lines will tell Excel to not calculate during the macro run.

Related

Excel VBA: efficient way to load data in a workbook with a lot of formulas

I have an Excel VBA code that reads the master data, load the data of each case/loop into a workbook, and then tons of calculations take place via numerous formulas (with no VBA codes). The desired result is the outputs of the calculations. The code loops through all the cases in the master data.
So here is the problem. I used multiple 'copy and paste' actions in each loop to load the data into the workbook, but the run time is way longer than I expected, causing Excel to be 'Not Responding'. Note that I already turned off automatic calculations in Excel and added the workbook Calculate trigger in the VBA code, so as to avoid updating the workbook entirely every time there is a paste action.
Could someone advise if directly setting the cells in the wb = the cells in the master data would speed up or slow down the code? Or could someone suggest a more efficient way of loading data?
I appreciate your effort before you send in your responses.
As said in the comments, not using .Select improves the speed of your macro. Also, copy paste actions are indeed more slow whith VBA and you should do assignations FromWorksheet.Range("CopyRange").Value = ToWorksheet.Range("PasteRange").Value. Also, it always helps to add these lines in the beginning:
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.DisplayAlerts = False
and reactivating them at the end:
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.DisplayStatusBar = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
There are many information on this, like this link

Will Application.ScreenUpdating = False speed up Excel macros with numerous .Select statements?

I've read that .Select statements force Excel to display each of those events on the screen and slow Macros considerably.
I'd like to speed up some old Macros that were written with record Macro methods and have many .Select statements.
Would Application.ScreenUpdating = False eliminate the time wasted displaying the .Select events?
Thanks in advance for any advice.
Of curse it would speed up your old Macros,but i would be better to change to code and avoid the .Select Statment. Because there is no need for Example to Select a Cell and then write to the Cell. It's more efficent to write directly to the cell.
Take a look at this entrys. They will give you help optimizingyour VBA Code.
Optimizing VBA macro
How to prevent a macro from freezing / turning white the Excel window?
How to avoid using Select in Excel VBA

VBA Paste values - Response time

I have a code that copies one range to another
rng1.Copy Destination:=rng2
However I only want the values, not all the formatting, so I have the below which works to achieve this
rng1.Copy
Application.ScreenUpdating = False
rng2.PasteSpecial Paste:=xlPasteValues
Application.ScreenUpdating = True
However, the processing speed of this is painfully slow. The sheet grinds to a halt for quite a long time compared to the instant response of the 1st formula.
Is there a method of amending the 1st formula to paste values only? I was unable to find a way in similar questions about pasting values....
Alternatively, is there another method that is quicker than using the 2nd formula?
copy & paste is notoriously slow in VBA, better to just assign directly, assuming both ranges are the same size
rng2.value = rng1.value

VBA Calculations Turning Back On Mid Execution

I'm running a large macro that includes various sub queries and functions.
Despite having the standard code to turn off calculations while the macro is running, somehow calculations are turned back on during the code execution.
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
I've added the above code the beginning of each major sub but still have the same issue. There's one point in the macro where I calculate the application to update a worksheet stored value, but I can't imagine that's what's causing it.
application.Calculate
Trying to find if there's a work around.

VBA Table Refreshing

I'm creating a worksheet in Excel that is connected to an online database. I made a function that checks if new information has been posted and updates the worksheet accordingly. If there is a new release, I update all the column headings to make room for it using a loop.
My problem is that the function takes forever to run since the table refreshes itself each time I change the column headings.
I'm wondering is there is a way to pause the refresh so the table only refreshes itself once at the end of the vba script.
Thanks. I appreciate your help.
To stop formulas from refreshing whilst the code executes, you can use Application.Calculation like so:
Public Sub SomeProcedure()
Application.Calculation = xlCalculationManual
'Code goes here
Application.Calculation = xlCalculationAutomatic
End Sub
This will stop the formulas refreshing until the code in the middle has executed.