View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Patrick C. Simonds Patrick C. Simonds is offline
external usenet poster
 
Posts: 343
Default Setting a public variable

So I Created a module (module1) and selected General : Declaration and
inserted:

Public myLocked As Boolean

Option Explicit

Then placed your suggested code in the CommandButton1_Click() event


Private Sub CommandButton1_Click()

On Error GoTo Termination

If Me.OptionButton1.Value = True Then
myLocked = True
Unload UserFormAccess
Call Protection.protect_all_sheets
Else
myLocked = False
Unload UserFormAccess
UserFormPassword.Show
End If

GoTo LastLine

Termination:

LastLine:

End Sub


Then added the If myLocked = True line in the routine below, but the If
statement does not run.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)

If myLocked = True Then

If SaveAsUI = True Then Cancel = True

End If

End Sub


"Dave Peterson" wrote in message
...
In a General module (not behind a worksheet and not under the userform):

Public myLocked as boolean

Then you can just use that variable in any procedure--like the Ok button
or the
Cancel button or wherever you want.

I wouldn't use Locked as a variable name. It looks way too close to a
Range's
.Locked Property name. And it may not confuse VBA, but it could confuse
me.

if me.optionbutton1.value = true then
mylocked = true
elseif me.optionbutton2.value = true then
mylocked = true
end if

Or you may be able to just look at the first and know (only two options
and one
must be selected???):

if me.optionbutton1.value = true then
mylocked = true
else
mylocked = false
end if

or its equivalent:
mylocked = cbool(me.optionbutton1.value = true)


"Patrick C. Simonds" wrote:

On Userform1 I have 2 OptionButtons.

I need some way to set a public variable (called Locked) to true if
OptionButton1 is true when Userform1 closes or to false if OptionButton2
is
true


--

Dave Peterson