View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default How to make error handle for mis-typed variable?

OK, I have an add-in with a public variable:
Dim VarABC as string

Now a Sub gets imported into the VBE from a text file. This textfile is just
completely unrelated to the add-in,
so the variables can be anything.
Now in this Sub in the textfile is a variable VarABZ. This really should be
VarABC, but it isn't.
Now when this Sub is imported the variable VarABZ isn't declared anywhere
(as the variable should be VarABC)
and there will be a compile error.

Can't make it any clearer. The more I think about it, the more I think there
is no solution for this other than
somehow scanning the whole textfile before importing it in the VBE.


RBS

"Dave Peterson" wrote in message
...
I don't understand this sentence:

"Then VarABZ is nowhere declared in the add-in and the compile error
occurs."
Because you said you had this in the addin:

In Add-in:
Public VarABC as String

==
In my simple test, I had this in a text file:

Option Explicit
Sub test01A()
VarABC = "hi"
MsgBox VarABC
End Sub

In my other workbook, I had this in module1:

Option Explicit
Public VarABC As String
Sub ImportTheTextFile()
Dim myFileName As String
myFileName = "C:\my documents\excel\test1.bas"
ThisWorkbook.VBProject.VBComponents.Import myFileName
Application.Run ThisWorkbook.Name & "!test01a"
End Sub

And it worked fine (xl2003).

RB Smissaert wrote:

I am not sure how that would work.
In it's most simple form this is how the situation is:

In Add-in:

Public VarABC as String

Sub RunImportedSub()
Application.Run strImportedSub
End Sub

In text file:

Sub strImportedSub()
VarABZ = "test"
End Sub

Then VarABZ is nowhere declared in the add-in and the compile error
occurs.
I can't alter the text files as they are with different people in
different
places.
All I can do is alter the add-in to deal with this.

RBS

"Dave Peterson" wrote in message
...
If they're public variables, maybe you could put them in the same
module
so that
their declaratations get imported, too.

But I think I would try to rewrite the sub so that any variables that
it
needs
are passed as parameters.

Then I could just pass the parms I need in the line that calls the sub.

RB Smissaert wrote:

Do I conclude then that there is just no solution for this?

RBS

"RB Smissaert" wrote in message
...
Have the following situation:
An .xla file will import a Sub from a text file and then run it like
this:

Dim strSub as String

'some code here to get the Sub imported
Application.Run strSub

Now it is possible that in this imported Sub are variables that are
not
defined publicly, so there
will be a compile error, variable not defined.
As this is not a runtime error I am not sure this error can be
handled
gracefully, but I would be interested
in any suggestions how this could be done.


RBS


--

Dave Peterson


--

Dave Peterson