VBA code stops in the middle of a for-next loop through a table
We have an Excel file that loops through a table and based on certain parameters, changes something in a record (row). This looping is in a VBA subroutine, that is part of a bigger subroutine that is triggered through the task scheduler.
We’ve encountered a few times now that the execution stops somewhere in the middle of the loop (we now have an elaborate logging system). And when the task is started again within 5 minutes, it runs smoothly. Which makes me think that it’s not necessarily the Excel file that has a problem, but maybe something with the machine that it’s running on?
The problem is also that the file will stay open and potentially is in the way of other tasks that are scheduled after it. We do use a kill-excel command before most tasks, but that can lead to Excel asking to Recover files and wanting an answer (that the task scheduler can’t give).
What could be going on? What questions do I have to ask to what specialists to solve this issue?
Edit: clarification on the task-scheduler part.
1. the scheduler is time triggered (00:45)
2. it executes a bat-file that in essence first kills existing instances of Excel and Outlook, then opens the Excel that’s one of the parameters and runs a subroutine that is also one of the parameters
This subroutine consists of a few steps
3. determine if it’s a bank holiday (updating a power query to an calendar in another file) – exit sub if yes
4. load prices (updating a power query to the prices file)
5. determine if all prices that are needed are actually there (excel formula) – exit sub if not
6. based on the new prices and information that is in each record already there is a helper column that is either true of false, this subroutine loops through the whole database and where the helper column is true, it changes some of the existing values. And in this subroutine the execution stops, sometimes after 10 true’s, or 5 or 20. And when there are no true’s, it goes onto the next subroutine
7-8-9-10. again a loop through the whole database now with 4 other helper columns.
So far, when the 6th step was done, it got through to the end (= sending out internal e-mails).
A few time I’ve watched it going ‘live’ – and stopping ‘live’ too (cmd window still open, excel open, unresponsive). After killing it all, I just ran the task again, without changing anything, and it would go smoothly.
That’s why I suspect it’s maybe something happening on the machine that halts all this?
I can’t share the file or the VBA code, not even with dummy data, as it would reveal our business formula’s.
Could the syncing of SharePoint/OneDrive be in the way? But if so, why not every day but seemingly random?
SledgeNL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.