ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Declare a public variabl (https://www.excelbanter.com/excel-programming/357532-declare-public-variabl.html)

Patrick Simonds

Declare a public variabl
 
I have a userform which contains a Calendar Control. When I select a date on
the Calendar Control I want Calendar.Value to be public so that it can be
used in another module.



Greg Wilson

Declare a public variabl
 
Have a public variable in a standard module:

Public DVal As Date

Your UF code should be like this:

Private Sub Calendar1_Click()
DVal = Calendar1.Value
End Sub

After selecting a date and closing the UF, if you run the following you
should return the selected date:

Sub Test()
MsgBox Format(DVal, "d mmm yyyy")
End Sub

However, note that if you are invoking an error somewhere it will kill
public variables. If the above doesn't work then you likely have one.

Regards,
Greg






"Patrick Simonds" wrote:

I have a userform which contains a Calendar Control. When I select a date on
the Calendar Control I want Calendar.Value to be public so that it can be
used in another module.




Kletcho

Declare a public variabl
 
declare a global variable by using the global key word at the top of
your module.

Global strMyGlobalString as string

Then in your sub procedure simply use the variable as you normally
would:

strMyGlobalString = Calendar.value


Patrick Simonds

Declare a public variabl
 
Not working and I am not sure why. The problem is that when I select a
Thursday (day 5) the Thursday_Test macro should place the value of DVal in
the active cell and then exit module. But it continues to the code after the
End If statement.

Here is my code:

From the Calendar UserForm:

Public DVal As Date

Private Sub CommandButton1_Click()
'Apply Change

DVal = Calendar1.Value
DateTest.Thursday_Test

End Sub

From the Thursday_Test Module:

Sub Thursday_Test()

Dim MyDate, MyWeekDay
MyDate = DVal 'Assign a date.
MyWeekDay = Weekday(MyDate)

If MyWeekDay = 5 Then
ActiveCell.Value = DVal
Exit Sub

End If
Unload Calendar
WrongDay.Show

End Sub















"Greg Wilson" wrote in message
...
Have a public variable in a standard module:

Public DVal As Date

Your UF code should be like this:

Private Sub Calendar1_Click()
DVal = Calendar1.Value
End Sub

After selecting a date and closing the UF, if you run the following you
should return the selected date:

Sub Test()
MsgBox Format(DVal, "d mmm yyyy")
End Sub

However, note that if you are invoking an error somewhere it will kill
public variables. If the above doesn't work then you likely have one.

Regards,
Greg






"Patrick Simonds" wrote:

I have a userform which contains a Calendar Control. When I select a date
on
the Calendar Control I want Calendar.Value to be public so that it can be
used in another module.






Greg Wilson

Declare a public variabl
 
Did you declare DVal in a standard module as opposed to the UF code module? I
have your code working provided it is declared in a standard module and the
Thursday_Test macro is in a standard module named "DateTest" and the UF is
named Calendar.

You could have the Thursday_Test macro contained in the UF code module and
declare DVal in the UF code module instead. It wouldn't have to be public in
this case.

Regards,
Greg


"Patrick Simonds" wrote:

Not working and I am not sure why. The problem is that when I select a
Thursday (day 5) the Thursday_Test macro should place the value of DVal in
the active cell and then exit module. But it continues to the code after the
End If statement.

Here is my code:

From the Calendar UserForm:

Public DVal As Date

Private Sub CommandButton1_Click()
'Apply Change

DVal = Calendar1.Value
DateTest.Thursday_Test

End Sub

From the Thursday_Test Module:

Sub Thursday_Test()

Dim MyDate, MyWeekDay
MyDate = DVal 'Assign a date.
MyWeekDay = Weekday(MyDate)

If MyWeekDay = 5 Then
ActiveCell.Value = DVal
Exit Sub

End If
Unload Calendar
WrongDay.Show

End Sub















"Greg Wilson" wrote in message
...
Have a public variable in a standard module:

Public DVal As Date

Your UF code should be like this:

Private Sub Calendar1_Click()
DVal = Calendar1.Value
End Sub

After selecting a date and closing the UF, if you run the following you
should return the selected date:

Sub Test()
MsgBox Format(DVal, "d mmm yyyy")
End Sub

However, note that if you are invoking an error somewhere it will kill
public variables. If the above doesn't work then you likely have one.

Regards,
Greg






"Patrick Simonds" wrote:

I have a userform which contains a Calendar Control. When I select a date
on
the Calendar Control I want Calendar.Value to be public so that it can be
used in another module.







Patrick Simonds

Declare a public variabl
 
Thanks for your help. That was my problem (well one of many) I had not
declared in a standard module.


"Greg Wilson" wrote in message
...
Did you declare DVal in a standard module as opposed to the UF code
module? I
have your code working provided it is declared in a standard module and
the
Thursday_Test macro is in a standard module named "DateTest" and the UF is
named Calendar.

You could have the Thursday_Test macro contained in the UF code module and
declare DVal in the UF code module instead. It wouldn't have to be public
in
this case.

Regards,
Greg


"Patrick Simonds" wrote:

Not working and I am not sure why. The problem is that when I select a
Thursday (day 5) the Thursday_Test macro should place the value of DVal
in
the active cell and then exit module. But it continues to the code after
the
End If statement.

Here is my code:

From the Calendar UserForm:

Public DVal As Date

Private Sub CommandButton1_Click()
'Apply Change

DVal = Calendar1.Value
DateTest.Thursday_Test

End Sub

From the Thursday_Test Module:

Sub Thursday_Test()

Dim MyDate, MyWeekDay
MyDate = DVal 'Assign a date.
MyWeekDay = Weekday(MyDate)

If MyWeekDay = 5 Then
ActiveCell.Value = DVal
Exit Sub

End If
Unload Calendar
WrongDay.Show

End Sub















"Greg Wilson" wrote in message
...
Have a public variable in a standard module:

Public DVal As Date

Your UF code should be like this:

Private Sub Calendar1_Click()
DVal = Calendar1.Value
End Sub

After selecting a date and closing the UF, if you run the following you
should return the selected date:

Sub Test()
MsgBox Format(DVal, "d mmm yyyy")
End Sub

However, note that if you are invoking an error somewhere it will kill
public variables. If the above doesn't work then you likely have one.

Regards,
Greg






"Patrick Simonds" wrote:

I have a userform which contains a Calendar Control. When I select a
date
on
the Calendar Control I want Calendar.Value to be public so that it can
be
used in another module.









Patricia Shannon

Declare a public variabl
 
Also, if you terminate a procedure with "end", it resets variables.
If you want to terminate the procedure other than by falling thru the "end
sub", use "exit sub".

"Patrick Simonds" wrote:

I have a userform which contains a Calendar Control. When I select a date on
the Calendar Control I want Calendar.Value to be public so that it can be
used in another module.





All times are GMT +1. The time now is 06:19 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com