0

I have inherited maintenance many excel sheets that are used by many people in my company. Recently one user has started getting application-defined errors which do not occur on other computers. I've debugged on his machine, and they happen when a worksheet is activated. It only occurs on worksheets with VBA in the sheet, and where there is an error in an unused routine. eg.

option explicit

sub RunReport()
... 
Setup.Activate 'compilation error occurs here when debugging
'I assume that this is the cause of the application-defined error which occurs when not debugging
...
end sub

workbook named Setup

Option Explicit

.... other code    

Private Sub ListBox1_Initialize()'Listbox1 has been deleted from the sheet so this is never called
        Dim allReports() As String
        allReports = Split(ALL_LOCS, DELIM)
        ListBox1.list = allReports 'compile error here because listbox1 doesn't exist
    End Sub

I assume that there is a setting that is calling a full compile of the code when the worksheet is activated. I want to turn it on on my dev environment so I can investigate these issues locally.
Where can I find this setting? (I am using office 2013)

Edit: For clarifcation based on answers. Option Explicit is set at the start of the code. I have autosyntax checking and require variable declaration set on my dev setup that does not experience this issue.

Phil
  • 397
  • 2
  • 8
  • What error is shown on what line precisely? – jkpieterse Jun 28 '17 at 09:28
  • when not debugging the application-defined or object-defined error message box occurs and the code stops. When debugging, the listbox1.list line gets a variable not defined error when setup.activate is called from another module (setup is the worksheet name) – Phil Jun 28 '17 at 09:34
  • remove that code then if the control doesn't exist, comment the code out, or remove it. – Nathan_Sav Jun 28 '17 at 09:44
  • I have done that in this instance, but there are many other workbooks that may be affected similarly (I've fixed 2 others already), and I'd like to find them myself – Phil Jun 28 '17 at 09:49
  • Do you know which workbooks are affected? If so, you could write code that manipulates their VBA project using the VBA extensibility library (after allowing programmatic access to the VBProject in the security center). This isn't very hard to do. – jkpieterse Jun 28 '17 at 10:11
  • Not all of them, at the moment its playing whack a mole as they get reported. If I can replicate the problem locally, I'll run them sequentially. – Phil Jun 28 '17 at 10:23
  • Actually, If I'm going to do that I may as well just do a full compile on each workbook to flush out any hidden errors. – Phil Jun 28 '17 at 10:25
  • I'll likely loop through all files in a folder and call compile detailed [here](https://stackoverflow.com/questions/28001620/can-i-compile-vba-on-workbook-open) I'll post code later. Will solve the overall problem but its not really an answer to my question, so I'll leave it open. – Phil Jun 28 '17 at 10:31

2 Answers2

1

The Option explicit on top of your modules forces a variable check, you can comment it if you don't want the variable check, or add it if you want it.

Note that this option can be added automatically in all new modules created, with a setting in the options:

VBE's Tools ► Options ► Editor ► Checkbox Require Variable Declaration

Graham
  • 7,431
  • 18
  • 59
  • 84
Thomas G
  • 9,886
  • 7
  • 28
  • 41
  • Thanks for the response. Option explicit is on at the start of the code (I'll update the question to include), and i have that setting on on my machine – Phil Jun 28 '17 at 09:23
  • @Thomas: The setting does not affect existing modules, it only ensure Option Explicit is added to newly inserted modules. – jkpieterse Jun 28 '17 at 09:27
  • Yes I think it need some clarification, I adapt my answer – Thomas G Jun 28 '17 at 09:36
0

From my experience, errors sometimes occur in VBA on other machines when You use in Your code some commands specific to x86 architecture when other machine is x64 and due to the fact that You use references (in VBA Tools->references) that are not available on other machines. Last cause that I've met were different security settings, e. g. maybe Your code uses sth that requires access to vba object model (in Excel File->Options->Trust center->Trust center settings->Macro settings). So first of all I recommend to check the Windows architecture on the suspected machine. Then check the references that You use in a workbook. And at the end check the trust center settings. I think that it might be very probable that You will not get the same errors on Your machine as they might be specific to only one machine (e. g. different system architecture, some specific references or trust center settings).

If the error is caused by a specific line in the code I understand that, e. g. there are two users of woorkbook and one deleted a tab, to which Your code reference and that causes the error. So I think that maybe a good approach for You will be to manage error. You can use On Error statements in vba, e. g. You have code that fires at workbook open and it runs three procedures from Modules, so You can do sth like this:

On Error GoTo Procedure1Error

here run procedure one

On Error GoTo Procedure2Error

here run procedure two

On Error GoTo Procedure3Error

here run procedure three

Exit Sub

Procedure1Error:

MsgBox ("Procedure 1 caused fatal error. Code was not executed.) Exit Sub

Procedure2Error:

MsgBox ("Procedure 2 caused fatal error. Code was not executed.) Exit Sub

Procedure3Error:

MsgBox ("Procedure 3 caused fatal error. Code was not executed.) Exit Sub

End Sub

If You do not notify user about problems then You could use the just On Error resume next (Your code will continue to execute) but I do not recommend it.

Mikisz
  • 404
  • 5
  • 20
  • Thanks for the comprehensive response. I will check if there are reference issues (mine is x64 and his is new, so probably x64 but could be it) – Phil Jun 28 '17 at 13:39
  • I also agree with error handling the offending code. These are inherited workbooks, which have quite a lot of technical debt - as evidenced by a deleted listbox but the event handler remaining. It's an on going process to improve them as I get time. – Phil Jun 28 '17 at 13:43