Jul 01 2022 02:30 PM
It's not that the macros run slowly, or that Excel crashes, but it just takes a while to get excel selected when coming from another program or webpage.
I tend to have to cut and paste a good deal of data into cells in the table I use. When it comes time to select the cell to paste the data into there is an annoying delay. When I remove all the macros it stops, but when I put them back in it starts again.
All of the macros are just ways to manage the view of the table - selecting criteria from filters on the headers and/or hiding columns I won't need to bother with.
I suspect that perhaps there is a better way to write my codes, but all my coding experience relies mainly on googling and adapting.
I'm also pretty new to this - so not too sure what would be the best way to post the codes I have for someone who might be willing to review. But here's what I use:
Unhide_All (I don't think this one is problematic)
Sub Unhide_All()
If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
Columns.EntireColumn.Hidden = False
Rows.EntireRow.Hidden = False
End Sub
Here's another for entering new information without having to look at columns and rows I don't need - my guess is it's one of the ones like this that are problematic:
Sub Referral_Entry()
Call Unhide_All
ActiveSheet.ListObjects("DG_TABLE").Range.AutoFilter Field:=1, Criteria1:= _
Array("Open Referral", "Approved", "Accepted"), Operator:= _
xlFilterValues
Dim Lastcolumn As Long
Lastcolumn = Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To Lastcolumn
If Cells(1, i).Value = "NOA Submitted" Or Cells(1, i).Value = "Auth #" Or Cells(1, i).Value = "1st Auth Day" Or Cells(1, i).Value = "LCD" Or Cells(1, i).Value = "CCR Due Date" Or Cells(1, i).Value = "CCR Submitted" Or Cells(1, i).Value = "DC Date" Or Cells(1, i).Value = "DC Submitted" Or Cells(1, i).Value = "Encounters Authed" Or Cells(1, i).Value = "Encounters Total" Or Cells(1, i).Value = "ART dates in DG" Or Cells(1, i).Value = "Injection Dates" Or Cells(1, i).Value = "H&P Sent" Or Cells(1, i).Value = "Signed H&P Uploaded" Or Cells(1, i).Value = "Billing Config Service Code" Or Cells(1, i).Value = "Bed Night Adjusted intake Date" Or Cells(1, i).Value = "Bed Night Adjusted DC" Or Cells(1, i).Value = "Mbr Bed Night Count" Then Columns(i).Hidden = True
Next
End Sub
I could post more, but I would bet if I can figure out the solution from one, I can repeat it for the others.
TIA!
Chileman
Jul 01 2022 09:06 PM
SolutionTry setting up your macro like this, it should actually be faster.
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Here insert your code
With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
.EnableEvents = True
End With
Example according to your code specifications.
'Example with your first code
Sub Unhide_All()
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
Columns.EntireColumn.Hidden = False
Rows.EntireRow.Hidden = False
With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Hope I was able to help you :).
I know I don't know anything (Socrates)
Jul 02 2022 11:25 AM
@CHileman , when does the sub Referral_Entry get called (run)?
Are you using any events like Worksheet_SelectionChange or Workbook_Activate etc?
Such events are fired too frequently and may become one reason of slow activation of Excel window or cell. If so, then may see if you can avoid using these events to run the subs. For example, you can choose to run the macros manually by adding them on QAT
Jul 06 2022 04:48 PM
Jul 06 2022 04:54 PM
Jul 06 2022 06:28 PM - edited Jul 06 2022 06:34 PM
'lags I was experiencing were not happening while a macro was running'
That's why I thought that some event might be slowing down the window/cell activation from another program.
Just to be clear, did disabling events just make the macro execution fast or did it also solve the Excel window/cell reactivation slow issue?
If it solved, then I share your (not so) mile long puzzle!
Otherwise, you can check if there are many macros installed, and is it some other macro which is depending on events to execute, and if those can be avoided as well.
Jul 07 2022 01:30 PM
Jul 07 2022 06:14 PM
Jul 08 2022 01:55 AM
Try to explain 🙂 in a few words why these commands could be used.
Please also consider that I do not know the formula and VBA structure of your workbook.
Calculation:
If you change cells/columns, etc. with VBA, to which functions of other cells refer, a recalculation is always started.
Depending on the functions and the associated number of calculations, it can have an impact (mostly negative) on performance.
While Excel is working on the recalculation, the processing of the program code is slowed down because the program flow only continues when the calculation has been completed for the time being.
You can avoid this problem by switching off the automatic calculation in the initial code.
EnableEvents:
The Select methods in the macro would re-invoke the macro called by moving the cursor around the worksheet. Setting the EnableEvent property to False prevents this from happening.
Disabling screen updating speeds up your macro code. While you won't be able to see what the macro is doing, it will run much faster. The ScreenUpdating property should end up being set back to True when your macro exits.
Excel VBA reference (here you can find general information about VBA)
Jul 08 2022 08:22 AM
@mtarler , Right? Your assumption is correct - no user defined functions in here. The only way to call any of the macros is manual, I don't event have buttons in the spreadsheet mapped to them, though I've considered adding that. The table itself has a few formulas in it, though the most complex of them is like three or maybe four IF s embedded (probably should use an array, but, well, got it working and don't want to look at it anymore). But it's nothing too complex, and the table itself isn't really all that large - maybe a few hundred rows, and about thirty columns.
So I still don't get why this helps. It makes perfect sense that the macros themselves would be more efficient shutting off auto-calculations, but why would it impact anything before any macro has been called?
I suppose I could play around and see if it really is the calculation part of the code update that provides the solution - take out the other two shut downs and see if performance is still improved. But I can't imagine the events have anything to do with it, unless there are events embedded within Excel itself I didn't purposefully add. And screen updates - I don't have fancy slide show effects built into any of it or anything of the sort.
But then, I probably ought to do my actual work first. Sometimes I think my real love of excel is that it helps immensely in my job, but it also provides escapes where I play with it instead of actually just using it. I'm sure I'm the only one like that though....
Jul 08 2022 08:27 AM
Jul 08 2022 08:47 AM
Jul 08 2022 08:56 AM
Jul 08 2022 09:05 AM
@CHileman A break point will stop code execution if/when it reaches it. In the VBA window simply click in the left margin of the code window on an executable line and a red dot will show up and the line will be highlighted:
then when that code runs it will stop at that point and you will have a chance to execute line by line, move execution to a different point, debug values, stop execution or just continue the run.
As for the comment I did not take it as deprecating at all, just truth. As an aside I recently learned that sarcasm is more 'hurtful intent' while facetious is not and in the past I sort of felt it was the other way around...
Jul 08 2022 10:05 AM
Jul 01 2022 09:06 PM
SolutionTry setting up your macro like this, it should actually be faster.
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Here insert your code
With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
.EnableEvents = True
End With
Example according to your code specifications.
'Example with your first code
Sub Unhide_All()
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
Columns.EntireColumn.Hidden = False
Rows.EntireRow.Hidden = False
With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Hope I was able to help you :).
I know I don't know anything (Socrates)