PDA

View Full Version : Public Variable won't pass



frankt
02-17-2018, 04:57 AM
Hi, I am new to the forum but hope that maybe some of you smart people might know the answer to my problem? I attached a test.xlsm file in which I
- first run the userform1 in which I declare a public variable and pass a value to it.
- then close that userform1
- and then want to display that variable again when I push the BTN2 button that is on Sheet1 of the excel file

Something goes wrong because the value of the public variable is not shown?
thanx is you can help!!

code for the userform:


Public chosendate As Date


Private Sub CommandButton1_Click()
chosendate = DTPicker1.Value
MsgBox chosendate
Exit Sub
End Sub




code for the button on Sheet1

Private Sub CommandButton1_Click()
MsgBox "the date = " & chosendate
End Sub

Dave
02-17-2018, 06:10 AM
U need to put the variable in a module. Declaring at the top of the userform only makes it available to all procedures in the userform. HTH. Dave

frankt
02-17-2018, 07:33 AM
thanx a lot Dave!

Dave
02-17-2018, 10:40 AM
You are welcome. Thanks for posting your outcome. Dave

SamT
02-17-2018, 11:03 AM
code for the button on Sheet1

Private Sub CommandButton1_Click()
MsgBox "the date = " & chosendate
End Sub




Private Sub CommandButton1_Click()

If UserForm1 Is Nothing Then Exit Sub
If UserForm1.chosendate = 0 then exit sub

MsgBox "the date = " & Format(UserForm1.chosendate, "dd/mm/yyyy")
End Sub