1

We have 2 macros - say M1 and M2.

The M1 macro is working correctly.

The M2 macro is not working as expected (there are no compile/run-time errors) and the reason for this is that 1 file is missing in this macro M2. Say F1.bas. This file has the foll. code:

Public Const REG_SZ As Long = 1

The other files in the macro refer to this REG_SZ. On Macro M1 if I right click on the REG_SZ in the other files and click on Definition, then it takes me to the F1.bas file on this line.

However, on the M2 although I have imported the F1.bas file, and compiled the code; If I right click on the REG_SZ in the other files and click on Definition, then it gives an popup that says: "Identifier under cursor is not recognized".

My understanding is that any public variable in the module file should be globally accessible. Is there something that I am missing to establish the link, do we need to do something else when importing a module file?

8
  • 1
    Some minimal reproducible code would be good in order to understand your problem? Why do you need the .bas file? Did you maybe do a syntax error? Commented Nov 22, 2018 at 7:30
  • No syntax error. Compiles OK. .bas file is needed because it has lot of more global variables. Its just basic code, it looks like the link is missing as explained in the question. I know that in some frameworks you need to include the other files, but in VBA my understanding is that anything declared as Public is global and available in any module file. Commented Nov 22, 2018 at 7:38
  • try something like: F1.REG_SZ instead of just REG_SZ Commented Nov 22, 2018 at 7:41
  • Yes, that works, but it will mean a lot of changes (find/replace) in the code which is not what I am looking; because the same concept works in the M1 macro by directly referring to REG_SZ, instead of F1.REG_SZ. ANy other suggestions please. Commented Nov 22, 2018 at 8:16
  • 1
    Every .bas should start with OPTION EXPLICIT. That will avoid you some situations where a module compiles but gives a runtime error. Commented Nov 22, 2018 at 9:53

1 Answer 1

1

May i suggest that instead of importing F1.bas, create a new module next to M1/M2 and copy contents of F1.

I was able to reproduce your problem, and copying the contents instead of just importing seems to be processed differently by the editor. As for the reasons why... I can't help.

Further testing shows that commenting your variable Public Const REG_SZ As Long = 1 and uncommenting it, forces the editor to recognise it again (from the imported file).

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you know if there is any way to identify such broken links (in my case there was no compile/run-time error)?
Could make a sub to debug.print each variable... can't think of a better way. Sub test() If REG_SZ <> 1 Then Debug.Print "REG_SZ link missing." End Sub
I always use Option Explicit at the top for my code, though not for quick testing like for your case... so yeah, that's another way, but it will pretty much stop you at one variable at the time... not particularly useful in your case, depending how often you need to import your modules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.