I have created a sub "importData", which takes as on optional parameter a string, which should the name of a worksheet to be read from. If it is empty, a default value set elsewhere should be used. More precisely, elsewhere is within the "Workbook_Open" sub.
When I open the workbook and run the importData macro (calling the sub without an argument), everything is fine.
Now, if I play a bit with VBA, not touching the above and VBA execution runs in an error elsewhere, the value of the default variable is dropped. If I run the sub after an error occured, my importData sub displays an error message, that the string is now empty, which is not a valid value of course.
A bit of code: Somewhere in the module, outside of any function or sub:
Public wsImporter As String ' Name of import worksheet.
Somewhere in the initialization sub being called by Workbook_Open:
wsImporter = "Import"
Beginning of sub "importData":
Public Sub importData(Optional ByVal importer As Variant)
(omitting local variable declaration)
If IsMissing(importer) Then importer = wsImporter
If Not chkWsExists(importer) Then
MsgBox "The specified import sheet does not exist: " & importer, vbCritical, "Microsoft Excel:importData"
Exit Sub
End If
...
End Sub
chkWsExists is just a boolean function returning true if the workbook contains a worksheet of the given name, false otherwise. It works fine. When I execute "importData" after a VBA error occured (completely ouside this, say a type mismatch while testing something else), then the Message Box appears and the value of importer is empty.
Essentially, it seems that the value of the public variable was somehow lost by the error. Is that normal behavior? Is there a way around it? Have I done something, to cause this?
This is an issue for me, since other sub's errors can lead to this sub not working anymore, e.g. there was an error caused elsewhere and the value of wsImporter got dropped, making it unusable for the normal user. Possible workaround would be close and open of workbook, but that is not very user friendly :)
Any insight in what's going on would be much appreciated.
Best,
YeOldHinnerk