Variable in VBA7

nd0911

Board Regular
Joined
Jan 1, 2014
Messages
166
Hello,

I share a lot of generic code between my projects I load a module and it "speaks" immediately with other modules, enum's and type's...

sometimes some specific module/enum/type is not on the workbook and I need to comment out a lots of line of code, so I thought to use some kind of public const as a "swich - On/Off" with VBA7, but its not working...


I want to do somthing like that, any idea ?

Code:
Public Const IsTechniqueOne As Boolean = True


Sub TestSub()


[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  IsTechniqueOne Then
    'do somthing with module a, b and c
    'module x,y,z is not on the workbook
    
    'use Enum and type - d,e,f
    'enum and type - g,h,i is not on the workbook
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    'do somthing with module x, y and z
    'module a,b,c is not on the workbook


    'use Enum and type - g,h,i
    'enum and type - d,e,f is not on the workbook
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If


End Sub
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Re: Variabl in VBA7

You set conditional compilation arguments (which is what IsTechniqueOne is) in the VBA project properties, not by declaring constants in the code.
 
Upvote 0
Re: Variabl in VBA7

Didn't know it was possible, although I prefer i could write some code like that:

Code:
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=publice"]#publice[/URL]  const IsBlaBala as boolean = True

Thank you !!
 
Last edited:
Upvote 0
Re: Variabl in VBA7

You could declare compilation constant directives in code as well

Code:
#Const [COLOR=#333333]IsTechniqueOne=True[/COLOR]

But I dn't think this would achieve what you want.

One possible workaround I can think of is to split the codes into two different routines (A and B) and then have a third routine that checks if the module exists in vbaproject and act accordingly .. something like this:
Code:
Function IsModule(ByVal ModuleName) As Boolean

    Dim oVbComp As Object
    
    On Error Resume Next
    Set oVbComp = Application.VBE.ActiveVBProject.VBComponents(ModuleName)
    IsModule = Not oVbComp Is Nothing

End Function


Sub Test()

    If IsModule("module1") Then
        Call A
    Else
        Call B
    End If

End Sub

The above steps should stop the compiler complaining.

Note that you will first need to allow access to the vbproject via the macro security menu.
 
Last edited:
Upvote 0
Re: Variabl in VBA7

Thanks Jaafar. I confess I had completely forgotten about the #Const directive.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top