I get the "out of memory" message when I execute my macro. It is too long to post but are there hints to better control the memory usage in Excel 2007? Getting rid of objects or something like this. I don't know how to do this.
best, U
Excel is notorious for this message and chances are, it has nothing to do with your computer's memory. I've had top of the line computers present this message with a 100kb excel program. The key COULD BE your userform and how certain parts get called. Best bet would be to run each event you have listed within your userform and see which one is the culprit.
Things to note:
Call
userform1.show vbmodeless
after all the textboxes, and other controls are set.
That sometimes works.
...Basically I have a lot of subroutines
to format and populate a worksheet
depending on criteria I define in a
userform.
I agree with #shahkalpesh comment. However, if you're looking for a strategy to help pinpoint the problem, you could try putting message boxes after you call each sub-routine from your main routine and observe excel's memory in the Task Manager.
Perhaps this will help you single out the offending code. Once found, you could post the code in here to resolve your issue.
Related
I have written a fairly complex application that uses Excel (2016) as front-end application for data stored in a MS Access database, using the ADO 6.1 library. I have noticed during macro code execution that SQL transactions triggered by my Excel application can take quite long to complete, and often the execution of the next line of code in my Excel macro depends on this SQL transaction first being completed. Unfortunately, macro code execution and SQL transactions are asynchronous operations, which means that the macro jumps to the next line of code even though the SQL transaction hasn't been completed.
My current work-around is to use a Sleep() function using the Windows API to insert a fixed delay, but this is a really ugly solution as it does reduce the performance of my application and depends very much on the CPU load, so it may sometimes work, sometimes not.
I haven't been able to find so far a solution to this problem, I can't find any hints on the Internet either.
Using Application.CalculateUntilAsyncQueriesDone doesn't help here either.
Does anyone have an idea or a hint how to halt macro code execution in Excel until an SQL transaction has been completed? Is there a method in ADO to check the completion of an SQL transaction?
Is your query within the Data/connections section?
I had this problem to, I turned off "Enable Background refresh" and added "DoEvents" to the VBA, this forces the data connection to refresh before it allows the code to continue. Downside to this is it makes excel feel like its locked up, But this resolved my issue.
Sub Button1_Click()
ActiveWorkbook.Connections("ScrapData").Refresh
DoEvents
....Other code....
End With
I have a relatively large (7MB) but more importantly formula and macro heavy excel file that I have been developing recently.
For the end user it must be able to be used in Automatic Calculation mode.
While calculating the whole sheet in Manual Calculation mode using F9 (Calculate Now) will work and calculate in ~10 seconds, but when I turn it onto Automatic mode, it will calculate repeatedly (i.e reaches 100% and then immediately starts again from 0%) and so freezes indefinitely.
So far I have tried:
Putting break points in all the VBA macros to see if it is hanging inside a macro
Removing all of the macros from the file (I was worried one of them was being triggered to run repeatedly in automatic mode)
but neither has worked leading me to wonder if the issue is not in fact VBA related.
Does anyone have any ideas about:
What might be causing this?
How I might diagnose the cause?
Happy to give more context if helpful. I am a relatively experienced excel user, and while generally don't write VBA macros from scratch am pretty confident at re-purposing code I inherit / find online. I am using a relatively powerful 6 core machine, but have tested it on others to the same results.
The issue turned out to be a volatile action that a macro was triggering. I had a macro that looped through hiding empty rows, but hiding a row is volatile, so then triggers a new calculation before the next run through the loop. The loop itself was 500 iterations, so that meant 500 sets of 3 second calculations!
See this link for more on volatile actions:
https://msdn.microsoft.com/en-us/library/bb687891(v=office.15).aspx
I have the following code in a Form Module in an xlsm:
Public Sub DoModal() ' Note: Called by LaunchHelper which is associated with F12 in Workbook_Open
Dim ewsActive As Worksheet: Set ewsActive = ThisWorkbook.ActiveSheet
If ewsActive Is Overview Then ' Overview is the CodeName of one of the Worksheets
Set ewsOverview = ewsActive ' This is a Private variable in the Form Module
ShowSafe
End If
End Sub
Private Sub ShowSafe() ' Calling Show directly caused random crashes, I concluded that these were caused by that the Form was not loaded into memory, this function makes sure, it is in memory before calling Show
Dim blnLoaded As Boolean: blnLoaded = False
Dim objForm As Object: For Each objForm In UserForms
If objForm.name = Me.name Then
blnLoaded = True
Exit For
End If
Next objForm
If blnLoaded = False Then
Load Me
End If
Show
End Sub
As far as I know:
VBA is single-threaded, it can never happen that two Subs or Functions are executed in parallel (e.g. while processing Worksheet_Activate, Worksheet_SelectionChange is also called, and the order of executing the statements inside those two functions is undetermined).
However, if we look at Excel+VBA as a system, then it is multi-threaded because Excel and VBA run in parallel (and Excel is multi-threaded itself, in addition).
My problem is:
I see a race condition in DoModal, because checking and acting is not atomic, and the following scenario is possible:
(1) The condition ewsActive Is Overview is evaluated as true, so we start executing the branch inside the If block.
(2) The execution is taken over by Excel, where the user switches to a different Worksheet.
(3) ShowSafe is called while we the ActiveSheet is not Overview but something else.
Do you agree with my reasoning? Is it correct that in spite of the checking, the Form might show up on a different Worksheet? So far, I have not succeded in making this error happen but I would like to know if it was possible in theory, even if it has only a low chance.
Where can I find more information about problems and guarantees related to multi-threading in VBA?
For the user to switch the ActiveSheet, Excel needs to process User Events. Experienced VBA programmers usually let Excel process user events by explicitly calling DoEvents, because Excel's GUI freezes/starves for CPU while VBA code is running. This suggests that the race condition you describe is extremely unlikely, if possible at all.
However, this non-concurrency between Excel's GUI and its VBA runtime is only know by practice. I didn't find official documentation that formally confirms that there's absolutely zero chance for Excel to process user events while VBA code is running (without explicitly yielding). We only know by practice that this chance is "almost zero".
For this reason I would say that your suggested race condition is, though extremely unlikely, theoretically possible until we have an official statement or documentation that rules out that concurrency.
p.s. I consider this question as of academic interest only, and the code of illustrative purpose only, because obviously there no relevance for a form to "check" whether it is actually loaded.
I'm not seeing what you're trying to do. Why does it matter what sheet is active when you show your user form? If it needs to be Worksheets("overview") then just Activate it on form Load event? It won't hurt if it's already active.
Similarly if you aren't sure if it's loaded then call Load just before your Show
Finally you're referring to the user form as Me - which implies that you're running this from the user form itself. So this code will not run unless the form is loaded into memory, so ShowSafe is pointless code.
I have a problem with the Excel macro I created. The macro is quite complicated (7 modules with ~2500 lines of code) and it's used to automatically assign pallets to trucks and sort them properly. It works perfectly for 99% cases but when recently it had to assign more than 1200 pallets for one direction it stopped working.
The weird thing is that normally it can process way more than that but only if it is splitting for different directions. Also it crashes not in one place but sometimes when building pallet number 1108 sometimes 1110 etc. Between 1108 and 1111.
Also the crashing is quite weird. Before I turned on the "Break on all errors" It was just exiting the Sub that it was at that time working on, going back to the first one and then processing the rest of the code without executing it (ignoring every variable changes, every If etc.) The only line that was working was the Message Box. Also Excel was unresponsive afterwards.
I am declaring every variable, I have Option Explicit on top of every module I am using few Public variables and a lot of Local? (the ones that are across the Module not across the Project).
It is crashing mostly at this line:
w2.Cells(r, 4).delete Shift:=xlUp
where w2 is set as:
Set w2 = Workbooks(wb1).Worksheets("SDP Temp")
Have you ever encountered something like this?
0x80010108 RPC_E_DISCONNECTED The object invoked has disconnected from its clients.
Is what it means. COM uses RPC for out of process.
I managed to find a fix for the problem.
When the code was executing he was never truly exiting the SUBs until the directory was finished.
Because of that when there was more than 28 trucks it had around 50 subs opened in the background.
When I fixed the code so the subs were properly closed after they were used it started working properly.
I am getting a 'Code execution has been interrupted' message on exit from excel intermittently of late. And its frequency is increasing. End allows me to leave excel, but once I get the message, my machine must be rebooted to allow for excel to open and run macros again. This intermittent message does come up with basic excel usage at exit, and is not limited to excel sessions following VBA macros use. Has anyone seen this or have a solution. It is getting very annoying.
I have came across this issue few times during the development of one complex Excel VBA app. Sometimes Excel started to break VBA object quite randomly. And the only remedy was to reboot machine. After reboot, Excel usually started to act normally.
Soon I have found out that solution to this issue is to hit CTRL+Break once when macro is NOT running. Maybe this can help to you too.