new to VBA--Trying to use Sheet properties like I would use Getters and Setters in OOP languages. I have the following code in Sheet1(simplified for clarity):
Option Explicit
Private bAllowChange As Boolean
Public Property Let AllowChange(bLetAllowChange As Boolean)
bAllowChange = bLetAllowChange
End Property
I would expect that when the AllowChange
property is called by a sub in another module, the bAllowChange
field will be updated accordingly. By using the debugging feature of VBE I can see that AllowChange
is being passed the correct value (bLetAllowChange
takes on the correct value) but the line
bAllowChange = bLetAllowChange
does not assign the value to bAllowChange
. If I set bAllowChange
to Public then it works as expected, however this defeats the purpose of using Properties in the first place. This is probably something simple that I'm not understanding about scope in VBA. Any suggestions? Thanks in advance.
The following works for me:
In Module1:
In the sheet with a code name of
Sheet1
:With that code set up, I can invoke the
SetItToFalse
subroutine and then every selection change shows me it isFalse
. After invokingSetItToTrue
every selection change shows me it isTrue
.You can use the Worksheet's Names collection to store your custom Worksheets property values permanently.
The main caveat to this is that the value is returned prefixed by an equal sign. (e.g. after setting
name.Value = True
, name.Value would ==True
). I made a workaround for this in my example.if you want persistent properties, try this (just an example of how to set/get/update/delete custom properties)