Excel macro inconsistently causes Excel to "stop working" - excel

I'm running Excel 2013 and a recent macro that I've developed is inconsistently (hooray!) causing Excel to freeze, giving me the "Excel has stopped working" error. In short, the macro prompts the user for some basic info about what they want to do and then creates a number of new sheets (based on a Template sheet) and populates each with rows of info from a Summary tab (which the user fills out before starting the macro). Every now and then when I run the macro, it freezes as soon as I complete the prompts. I can immediately restart the workbook, run the macro again with the same inputs and it will work. So it’s hard to replicate the problem, making it hard to diagnosis and fix it.
I've tried a number of things to fix this (changing Shift:=xl--- calls to xlShift---; changing the tabs from Page Break Preview to Normal view; moving the macro button to another sheet), but it still freezes. The latest attempt (which seemed to be working for a while) was to change the Form Control button used to start the macro to an ActiveX Control button (which is housed on the Summary tab). I tried setting up a basic error handler, but that didn’t find anything when I got it to freeze again. I’ve tried stepping through the code and when it froze, it was after executing a selection.insert call the first time.
I’ve included the full macro below, but the (potential) problem line is:
Sheets(yr_temp).Rows(3 + task_counter).Insert (xlShiftDown)
Any ideas of what might be going on and what else I can try to fix this problem? The only thing I haven't fully tested is to insert Application.Wait Now + (TimeValue("00:00:01") after the .Insert line; another user said this worked for them (can't find the link at the moment UPDATE #4: Found it! Here's the link.), though it was a slightly different situation. Also, that line can run hundreds of times on a single execution of the macro, so waiting one second each time is pretty burdensome (I haven't tried using a fraction of a second, nor am I sure how short of a wait would still have the desired effect).
UPDATE #1: I forgot about this tidbit. When it freezes, it seems to always be on the first time I run the macro after opening the workbook. I don't believe it has ever frozen on the second or third or... time running the macro.
UPDATE #2: After adding debug.print calls for yr_temp and task_counter (thanks for the suggestion, Alex Bell), and running the macro many many times, I finally got it to freeze on me while I watched the Immediate Window. Again, it appeared to have crashed after the first call of the oddly troublesome sheets.insert line. More importantly, the values for yr_temp and task_counter were the same valid numbers from every previous attempt that ran smoothly. So, any other ideas what might be causing this problem? This weekend I will try to run this on another computer to see if it might be something with this system and not the macro itself.
UPDATE #3: I tried using the workbook on my other computer (running Office 2010). So far I haven't had it freeze, but it's too early for me to claim that it doesn't freeze on that system.
Sub Generate_MM_Sheets()
'' Turn off screen updating for duration of macro (to improve macro speed...and prevent seizures)
Application.ScreenUpdating = False
'' Declare all variables
Dim valid_resp As Boolean, user_resp As String '' For user prompts
Dim yr_start As Integer, yr_num As Integer, yr_counter As Integer, yr_temp As String '' For year values
Dim task_num As Integer, task_counter As Integer, task_temp As Integer '' For task numbers
Dim sheets_num As Integer, sheet_counter As Integer '' For sheet numbers
Dim proj_name As String '' For project name
Dim wb_current As String, wb_new As String '' For workbook names
'' Prompt user to define starting year for the Maintenance Manual sheets
valid_resp = False
Do
user_resp = InputBox("Enter the desired starting year for the Maintenance Manual sheets", , Year(Now()))
If user_resp = "" Then '' If the user hits Cancel or returns an empty response, restore screen updating and end the macro
Application.ScreenUpdating = True
Exit Sub
ElseIf Not IsNumeric(user_resp) Then '' If the response is not a number, warn the user and retry
MsgBox ("Doh! Invalid entry. The value you entered was not a number.")
ElseIf Not user_resp = Int(user_resp) Or user_resp <= 0 Then '' If the response is not a positive integer, warn the user and retry
MsgBox ("Aw snap! Invalid entry. The value you entered was not a positive integer.")
Else '' Otherwise the response is deemed valid. Set the response validity to true and define the macro variable as the user response
valid_resp = True
yr_start = user_resp
End If
Loop Until valid_resp = True '' Loop until a valid response is entered
'' Same logic as above, but this time to define the number of years for the Maintenance Manual sheets
valid_resp = False
Do
user_resp = InputBox("Enter the desired total number of years for the Maintenance Manual sheets", , 30)
If user_resp = "" Then
Application.ScreenUpdating = True
Exit Sub
ElseIf Not IsNumeric(user_resp) Then
MsgBox ("Come on! Invalid entry. The value you entered was not a number.")
ElseIf Not user_resp = Int(user_resp) Or user_resp <= 0 Then
MsgBox ("Bummer, dude! Invalid entry. The value you entered was not a positive integer.")
Else
valid_resp = True
yr_num = user_resp
End If
Loop Until valid_resp = True
'' Prompt user to define project name for use in the Maintenance Manual sheet headers
proj_name = InputBox("Enter the name of the project for the Maintenance Manual sheets")
'' Use the above responses, the data in the Summary tab and the template in the Template tab to create and populate the Maintenance Manual sheets
task_num = Range(Sheets("Summary").Range("A4"), Sheets("Summary").Range("A4").End(xlDown)).Rows.Count '' Count the number of Tasks in the Summary tab
sheets_num = ActiveWorkbook.Sheets.Count '' Count the current number of sheets in the macro workbook (this value is used when moving the generated sheets to new workbook)
Sheets("Template").PageSetup.CenterHeader = proj_name & Chr(10) & "Maintenance Items" '' Update the header of the Template tab to be used on the generated sheets
For yr_counter = 1 To yr_num '' Loop through each year
yr_temp = yr_start + yr_counter - 1 '' Determine the year value for this loop
Sheets("Template").Copy After:=Sheets(Sheets.Count) '' Copy the Template tab to the end of the workbook
Sheets("Template (2)").Name = yr_temp '' Rename the new tab based on the year value for this loop
Sheets(yr_temp).Range("A1").Value = Sheets(yr_temp).Range("A1").Value & yr_temp '' Revise the new tab's description based on the year value for this loop
task_counter = 0
For task_temp = 1 To task_num '' Loop through each task
task_rem = (yr_counter + Sheets("Summary").Range("E4").Offset(task_temp - 1, 0).Value) Mod _
Sheets("Summary").Range("D4").Offset(task_temp - 1, 0).Value '' Check if the task is due this year (i.e., the year count (plus task age) is a factor of the task frequency)
If task_rem = 0 Then '' Then, for each task that is due this year...
task_counter = task_counter + 1 '' Increment the counter for the number of tasks that have been identified as due this year
Sheets("Summary").Rows(3 + task_temp).Copy '' Copy the task from the Summary sheet and insert it at the bottom of the new tab
Sheets(yr_temp).Rows(3 + task_counter).Insert (xlShiftDown)
End If
Next task_temp
Sheets(yr_temp).Columns("D:E").Delete (xlShiftToLeft) '' Delete the frequency and current age columns from the new tab
Sheets(yr_temp).Rows(4 + task_counter).Delete (xlShiftUp) '' Delete the blank placeholder row (used to preserve formatting and print area) from the new tab
Next yr_counter
'' Move all of the newly generated Maintenance Manual sheets to a new workbook
wb_current = ActiveWorkbook.Name '' Note: This is used in the following code block, but needs to be performed here for simplicity
Sheets(sheets_num + 1).Select '' Select the first annual sheet
For sheet_counter = 2 To yr_num '' Add each of the remaining annual sheets to the selection
Sheets(sheets_num + sheet_counter).Select False
Next sheet_counter
ActiveWindow.SelectedSheets.Move '' Move the selected sheets (hopefully all of the newly generated annual sheets) to a new workbook
'' Restore the macro workbook tabs to their original state (for aesthetic/convenience reasons) and then focus back to the newly created workbook
wb_new = ActiveWorkbook.Name
Workbooks(wb_current).Sheets("Instructions").Activate
Workbooks(wb_current).Sheets("Summary").Activate
Workbooks(wb_current).Sheets("Template").PageSetup.CenterHeader = Chr(10) & "Maintenance Items" '' Remove project name from Template tab header
Workbooks(wb_new).Activate
'' Restore screen updating
Application.ScreenUpdating = True
End Sub

I pulled up the error detail reports for the freezes through the Action Center and it looks like the "fault module" is mso.dll (at least it was in the few error reports I checked). The most promising suggested fix that I found was to run a repair of Office (through Programs and Features in the Control Panel). So far this has been working, but I won't call this one solved until I get in a couple more days of use without crashes.
UPDATE: I've been testing it out for a couple days now and still no crashes. I'm marking this as the accepted solution, but will update if the crash comes back. For anyone interest, here is one of the error reports:
**Source**
Microsoft Excel
**Summary**
Stopped working
**Date**
‎12/‎11/‎2014 4:55 PM
**Status**
Report sent
**Description**
Faulting Application Path: C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE
**Problem signature**
Problem Event Name: APPCRASH
Application Name: EXCEL.EXE
Application Version: 15.0.4569.1504
Application Timestamp: 52c5e9e1
Fault Module Name: mso.dll
Fault Module Version: 15.0.4569.1506
Fault Module Timestamp: 52e0c1d0
Exception Code: c0000602
Exception Offset: 0116b30f
OS Version: 6.1.7601.2.1.0.256.4
Locale ID: 1033
Additional Information 1: 6e8a
Additional Information 2: 6e8ae308d57954ab0513d50f2363e5fc
Additional Information 3: 8248
Additional Information 4: 8248af4ab5d8a2564e54f9d6dd7f5d2b
**Extra information about the problem**
Bucket ID: 94371593

Pertinent to your case, it's hard to perform remote debugging without having the actual data. As a general recommendation, apply debugging technique to that problematic statement in you code, like inserting:
' debugging: output to be sent to Immediate Window
Debug.Print task_temp
Debug.Print yr_temp
Debug.Print task_counter
' rest of your code, starting with problematic part
Sheets("Summary").Rows(3 + task_temp).Copy
Sheets(yr_temp).Rows(3 + task_counter).Insert (xlShiftDown)
Analyze the validity of values prior to the execution of that problematic line, in particular: if the row numbers are withing the valid range, and if the Sheets(yr_temp) exists.
Hope this will help to trace down the source of the Error.

Related

Store global variable in Excel even when closed

I have a question and I don't know if it has an answer.
What I would like to do is track the usage of a macro inside a global variable for an entire day. I want this macro to only be capable of running once a day. I choose if I run it in the morning, noon or evening.
So for example:
Today we are on the 13/01/2022. In the morning I ran this macro and closed the workbook. I want to not be able to run this macro before the 14th (a different day), even if I reopen the Workbook on the 13th in the afternoon, I press the macro button and it won't run.
So I want this macro to check if it was run today and then if the answer is yes to not run, but if the answer is no to run. Can this be done ? I thought about creating a global variable was_run, but how can I store its value even when the workbook is closed and reopen again ? Is this possible ?
Thank you for your help !
Please, try the next way:
Sub myMacroRunningOnlyOncePerDay()
Const myApp As String = "My Daily Variable", Sett As String = "Settings"
Const strDate As String = "myDailyValue"
Dim RegValue As String, NoRun As Boolean
RegValue = GetSetting(myApp, Sett, strDate, "No value")
If RegValue <> "No value" Then
If IsNumeric(RegValue) Then
If CLng(RegValue) = CLng(Date) Then NoRun = True
End If
End If
If NoRun Then Exit Sub
SaveSetting myApp, Sett, strDate, CStr(CLng(Date))
MsgBox "It runs..."
End Sub
It should be good to place the constants (Registry keys) on top of the module (in the declarations area). In this way you can use them from different Subs.

What are the reasons UnMerge will not work in Access VBA?

I have a VBA macro in Access 2016 (actually, written for earlier version, but right now I'm working with 2016). What it does is adjusting several Excel spreadsheets, and moving them to another location, while also writing some logs into SQL table.
One of adjustments is unmerging some merged cells. The weird part is, those excel files fail to process at times (not always), when this Access DB is triggered programmatically (from another macro in another Access DB). When I launch it manually, or go into debug step-by-step, it always works - so I am confused, and can't find the reason.
I tried to replace "MergeCells = False" for "Unmerge" - it's the same. I also tried to make new ACCDE file - no result.
This is part of the code, where the unmerging occurs.
xlsh.Select
xlsh.Range("A1:D1").Select
xlsh.Outline.ShowLevels RowLevels:=2
With Selection
.UnMerge
End With
xlsh.Range("A1").Value = "Alfa"
xlsh.Range("B1").Value = "Bravo"
xlsh.Range("C1").Value = "Golf"
xlsh.Range("D1").Value = "Tango"
xlsh.Range("A1").Select
If xlsh.Range("A1").Value = "" Then GoTo error
When it fails to unmerge the cell, the value won't get assigned, and at the end it's empty, thus, continues to label 'error'.
Try this one:
xlsh.("A1:D1").Unmerge
Instead of
xlsh.Select
xlsh.Range("A1:D1").Select
xlsh.Outline.ShowLevels RowLevels:=2
With Selection
.UnMerge
End With
The last 2 lines are unnecesary as:
If xlsh.Range("A1").Value = "" Then GoTo error
Will not be true as you are assigning a value a few lines before. (xlsh.Range("A1").Value = "Alfa")

VBA subroutine slows down a lot after first execution

I have a subroutine that generates a report of performance of different portfolios within 5 families. The thing is that the portfolios in question are never the same and the amount in each family neither. So, I copy paste a template (that is formated and...) and add the formated row (containing the formula and...) in the right family for each portfolio in the report. Everything works just fine, the code is not optimal and perfect of course, but it works fine for what we need. The problem is not the code itself, it is that when I execute the code the first time, it goes really fast (like 1 second)... but from the second time, the code slows down dramatically (almost 30 second for a basic task identical to the first one). I tried all the manual calculation, not refreshing the screen and ... but it is really not where the problem comes from. It looks like a memory leak to me, but I cannot find where is the problem! Why would the code runs very fast but sooooo much slower right after... Whatever the length of the report and the content of the file, I would need to close excel and reopen it for each report.
**Not sure if I am clear, but it is not because the code makes the excel file larger or something, because after the first (fast) execution, if I save the workbook, close and reopen it, the (new) first execution will again be very fast, but if I would have done the same excat thing without closing and reopening it would have been very slow...^!^!
Dim Family As String
Dim FamilyN As String
Dim FamilyP As String
Dim NumberOfFamily As Integer
Dim i As Integer
Dim zone As Integer
Sheets("RapportTemplate").Cells.Copy Destination:=Sheets("Rapport").Cells
Sheets("Rapport").Activate
i = 3
NumberOfFamily = 0
FamilyP = Sheets("RawDataMV").Cells(i, 4)
While (Sheets("RawDataMV").Cells(i, 3) <> "") And (i < 100)
Family = Sheets("RawDataMV").Cells(i, 4)
FamilyN = Sheets("RawDataMV").Cells(i + 1, 4)
If (Sheets("RawDataMV").Cells(i, 3) <> "TOTAL") And _
(Sheets("RawDataMV").Cells(i, 2) <> "Total") Then
If (Family <> FamilyP) Then
NumberOfFamily = NumberOfFamily + 1
End If
With Sheets("Rapport")
.Rows(i + 8 + (NumberOfFamily * 3)).EntireRow.Insert
.Rows(1).Copy Destination:=Sheets("Rapport").Rows(i + 8 + (NumberOfFamily * 3))
.Cells(i + 8 + (NumberOfFamily * 3), 6).Value = Sheets("RawDataMV").Cells(i, 2).Value
.Cells(i + 8 + (NumberOfFamily * 3), 7).Value = Sheets("RawDataMV").Cells(i, 3).Value
End With
End If
i = i + 1
FamilyP = Family
Wend
For i = 2 To 10
If Sheets("Controle").Cells(16, i).Value = "" Then
Sheets("Rapport").Cells(1, i + 11).EntireColumn.Hidden = True
Else
Sheets("Rapport").Cells(1, i + 11).EntireColumn.Hidden = False
End If
Next i
Sheets("Rapport").Cells(1, 1).EntireRow.Hidden = True
'Define printing area
zone = Sheets("Rapport").Cells(4, 3).End(xlDown).Row
Sheets("Rapport").PageSetup.PrintArea = "$D$4:$Y$" & zone
Sheets("Rapport").Calculate
Sheets("RANK").Calculate
Sheets("SommaireGroupeMV").Calculate
Sheets("SommaireGroupeAlpha").Calculate
Application.CutCopyMode = False
End Sub
I do not have laptop with me at the moment but you may try several things:
use option explicit to make sure you declare all variables before using them;
from what I remember native vba type for numbers is not integer but long, and integers are converted to long, to save the computation time use long instead of integers;
your Family variables are defined as strings but you store in them whole cells and not their values i.e. =cells() instead of =cells().value;
a rule of a thumb is to use cells(rows.count, 4).end(xlup).row
instead of cells(3, 4).end(xldown).row.;
conditional formatting may slow down things a lot;
use for each loop on a range if possible instead of while, or even copy range to variant array and iterate over that (that is the fastest solution);
use early binding rahter of late binding, i.e., define objects in a proper type as soon a possible;
do not show printing area (page breaks etc.);
try to do some pofiling and look for the bottlenecks - see finding excel vba bottlenecks;
paste only values if you do not need formats;
clear clipboard after each copy/paste;
set objects to Nothing after finishing using them;
use Value2 instead of Value - that will ignore formatting and take only numeric value instead of formatted value;
use sheet objects and refer to them, for example
Dim sh_raw As Sheet, sh_rap As Sheet
set sh_raw = Sheets("RawDataMV")
set sh_rap = Sheets("Rapport")
and then use sh_raw instead of Sheets("RawDataMV") everywhere;
I had the same problem, but I finally figured it out. This is going to sound ridiculous, but it has everything to do with print page setup. Apparently Excel recalculates it every time you update a cell and this is what's causing the slowdown.
Try using
Sheets("Rapport").DisplayPageBreaks = False
at the beginning of your routine, before any calculations and
Sheets("Rapport").DisplayPageBreaks = True
at the end of it.
I had the same problem. I am far from expert programer. The above answers helped my program but did not solve the problem. I'm running excel 2013 on a 5 year old lap top. Open the program without running it, go to File>OptionsAdvanced, Scroll down to Data and uncheck "Disable undo for large Pivot table refresh...." and "Disable undo for large data Model operation". You could also try leaving them checked but decreasing their value. One or both of these seem to be creating a ever increase file that slows the macro and eventual grinds it to a stop. I assume closing excel clears the files they create so that's why it runs fast when excel is closed and reopened at least for a while. Someone with more knowledge will have to explain what these changes will do and what the consequences are of unchecking them. It appears these changes will be applied to any new spread sheets you create. Maybe these changes would not be necessary if I had a newer more powerful computer.

VBscript and Excel: Opening and closing Excel objects - Arraylist preventing closing of object

I've got a problem with a vbscript which creates Excel objects and reads from an Excel file.
The vbscript is executed from an Excel macro, and then creates and opens the Excel file and reads from it. The problem is that the Excel object isn't allways closed, even though I'm trying to to it.
Here's the code from the vbscript:
Set ExcelObject = createobject("Excel.Application")
ExcelObject.workbooks.open testWorkBookPath
Set testActionArray = CreateObject( "System.Collections.ArrayList" )
Function getTestsCaseActions (testsPath, esheet, row, col)
Set my_sheet = ExcelObject.sheets.item(esheet)
tempArray = array(my_sheet.cells(row, col-2), "")
testActionArray.Add tempArray
Do While my_sheet.cells(row, col).Value <> ""
tempArray = array(my_sheet.cells(row, col), my_sheet.cells(row+1, col))
testActionArray.Add tempArray
col = col+1
Loop
End Function
getTestsCaseActions testWorkBookPath, testCaseSheet, 3, 4
ExcelObject.Quit
Now, if I run the above code and watch the process explorer, a new Excel process is spawned when the script is started, and then closes, as expected.
However, if I insert this code after running the function, before the ExcelObject.Quit line:
For Each ArrayItem in testActionArray
IF ArrayItem(1) = "" Then
Wscript.Echo ArrayItem(0)
Else
Wscript.Echo ArrayItem(0) & " -> " & ArrayItem(1)
End If
Next
ExcelObject.Quit (STILL HERE)
then the spawned process does NOT quit, and the process list grows until Excel goes completely bananas.
I don't understand this; All the last bit of code does is loop through the ArrayList and print the contents. Why's not the process quitting?
EDIT: At seems that at least some of the Excel objects eventually disappear from the Process Explorer, but this takes about 20-30 minutes. And it's just a few of them - most are still there. At least my list at the moment has shrinked some, but there are still about 15 Excel processes running.
Also, suddenly this message appears:
File Now Available
'filename.xlsm ' is now available for editing. Choose Read-Write to open it for editing.
This line seems to help. It doesn't completely remove all extra processes, but the number grows to five, then goes back to two, and so on. So it works pretty well.
dim book: for each book in ExcelObject.Workbooks: book.saved = true: next

VBA Code in Excel randomly stops executing. No error messages occur

Essentially, I have an Updata button that takes information from two columns, in two spreadsheets (within 1 book). The overall goal of this code is to take all the values from one column, and then append the values from the other column below it.
Worksheets("Overall Flow").Range("A4:A1004").Value = Worksheets("Active").Range("A2:A1002").Value
Dim i As Integer
For i = 4 To 1004
If Worksheets("Overall Flow").Range("A" & Trim(str(i))) = "" Then
Worksheets("Overall Flow").Range("A" & Trim(str(i)) & ":A" & Trim(str(1000 + i))).Value = Worksheets("Inactive").Range("A2:A1002").Value
i = 1005
End If
Next
For some reason, the first line executes, and then finishes. When I put break points, then do step-by-step, no other steps happen afterwards.
When I run the first line individually, it appears to work fine, but not when:
Worksheets("Overall Flow").Range("A" & Trim(str(i)) & ":A" & Trim(str(1000 + i))).Value = Worksheets("Inactive").Range("A2:A1002").Value
or
Worksheets("Overall Flow").Range("A4:A1004").Value = Worksheets("Inactive").Range("A2:A1002").Value
is present aftwards.
Solution to this is very unusual.
CTRL+BREAK CTRL+BREAK CTRL+BREAK ESC
It just happened to me againg after long time, I was looking for a solution and I came here then this sequence came back to my mind and I tried.
It worked for me, I hope this will help someone.
Update: Tweaked code (now with error checking!)
Main points concerning the current code:
When copying the ACTIVE range, check for last consecutive cell used. This is faster and more effecient than a loop.
Why are you trimming a number you know will not contain spaces?
There's no need to set i = 1005, just use Exit For. This is more effecient and clear to the reader what the intention is. I don't use this in the code below since I avoided looping altogether.
Here's a different way you can do this without any looping, which I think is more clear and effecient. Try this and see if it works for you:
Sub test()
Dim lastRow As Long, offSet As Long
lastRow = Worksheets("Active").Range("A2").End(xlDown).row
'Sanity checks
If IsEmpty(Worksheets("Active").Range("A2")) = True Then offSet = 1: lastRow = 2
If lastRow > 1001 Then lastRow = 1002
Worksheets("Overall Flow").Range("A4:A" & lastRow + 2).Value = _
Worksheets("Active").Range("A2:A" & lastRow).Value
If lastRow < 1002 Then
Worksheets("Overall Flow").Range("A" & lastRow + (3 - offSet) & _
":A1004").Value = Worksheets("Inactive").Range("A2:A1002").Value
End If
End Sub
Notes:
Sanity check 1 is for if A2 is blank in the Active sheet.
Sanity check 2 is for if there are cells beyond A1002 with values in Active sheet.
This is what I am using to test your code. Since I don't know what's in the spreadsheets, I can't reproduce exactly what you're seeing so I'm first putting dummy data into the ranges.
For me it is running fine every time, and I've tried it on 2 different computers - Excel 2003, and Excel 2010.
I set a breakpoint and stepped with F8, and also Shift F8 and both worked fine.
Something may be different with your data (i.e. the first cell being copied over from the inactive sheet is blank and therefore execution stops after processing the first cell -- check that column A4 is not blank), or perhaps some memory has gotten corrupted from having Office being killed.
In a Module I have:
Sub test()
Worksheets("Active").Range("A2:A1002").Value = "active"
Worksheets("Active").Range("A5").Value = ""
Worksheets("Inactive").Range("A2:A1002").Value = "inactive"
Worksheets("Overall Flow").Range("A4:A1004").Value = Worksheets("Active").Range("A2:A1002").Value
Dim i As Integer
For i = 4 To 1004
If Worksheets("Overall Flow").Range("A" & Trim(Str(i))) = "" Then
Worksheets("Overall Flow").Range("A" & Trim(Str(i)) & ":A" & Trim(Str(1000 + i))).Value = Worksheets("Inactive").Range("A2:A1002").Value
i = 1005
End If
Next
End Sub
Have you tried the same code on another computer?
I had this issue and I tracked it down to custom VBA functions used in Conditional Formatting that was processed while application.screenupdating was still set to True.
I'm not sure how consistent this behaviour is but, when a custom VBA function is referred to in a conditional formatting rule, when the screen updates, it will not step through the code even when employing break points or the debug.assert method. Here's the breakdown of what happened:
Context:
2 open workbooks.
Conditional formatting and custom function in question were in workbook1.
The code I was attempting to execute was in workbook2.
Process
I call a procedure in workbook2.
Workbook2's procedure reaches a line executing an autofilter command.
Autofilter command triggers a screen update in all open workbooks (any command that triggers a Worksheet_Change or Worksheet_Calculate event can apply here).
Screen update processes the conditional formatting rules, including the rule in workbook1 calling workbook1's custom function.
Custom function is run in a 'silent' state (i.e. with no interaction with user, ignoring break points and "debug.assert" calls; this appears to be by design as part of the conditional formatting feature)
Custom function finishes execution and ceases all other active code execution.
I fixed my problem by adding a Application.ScreenUpdating = False line at the start to prevent screen updates and, by extension, conditional format processing (but it's best to keep custom functions away from conditional formatting to begin with).
I'm not sure if this is relevant to your situation at all but I hope it helps somebody.
It has already been mentioned in transistor1's answer, but only as a side comment.
I had a similar problem, that VBA code simply stopped executing in the middle of a function. Just before that it also jumped back a few lines of code. No Error Message was shown.
I closed all open Excel programs, and upon reopening the File everything worked fine again.
So my confirmed Answer to this problem is: Corrupted Memory, restart Excel.
Edit: after doing this, I also encountered the Problem that Visual Basic Editor crashed when I tried uncommenting a particular line. So I created a New Excel file and copied my code. Now I don't have any problems anymore.
I ran into the same problem. I had a sub routine that gave random errors throughout the code without giving error messages. By pressing F8, the code would resume.
I found someone had posted a Subroutine he called "ThatCleverDevil" I do not remember the resource or who posted it. It would warn you an error was about to occur. The routine is posted below.
I split the code into component sub-routines. The short snippits ran with no interruption or erros. I created a subroutine that called each snippit. Errors resumed.
They would run individually, but not all together.
RESOLUTION: Between called sub-routines, I ran the following line of code:
Application.Wait Second(Now) + 1
The code then ran without error.
Thanks to whomever it was that wrote ThatCleverDevil. And special thanks to the coder who wrote about Application.Wait.
Sub ThatCleverDevil()
On Error GoTo err
MsgBox "About to error"
err.Raise 12345
MsgBox "Got here after the error"
Exit Sub
err:
Stop: Resume
End Sub
Robert
VBA simply is prone to this issue. I have used it for years in corproate workflows because it is so hardcoded into lots of things, but if possible I would just consider alternatives. If this an ad-hoc project R will be faster and offer more flexibility. If this is more production oriented and meant to handle large volumes I would consider informatica.
To improve the performance I called the function DoEvents inside the loop. It solved the problem for me.

Resources