Solved

VBA: how to return a value from a pick-a-date-frmo-calendar sub

Posted on 2014-07-22
8
975 Views
Last Modified: 2014-07-23
I successfully used the following code in a VBA script to let the user pick a date from a calendar:

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
    On Error Resume Next
	MsgBox (DateClicked)
    Unload Me
End Sub

Open in new window



Question is: how can I read that value DateClicked from another sub?
If I launch the MsgBox (DateClicked) inside the sub above I see a date, while
If I launch it from another sub I see an empty msgbox :(
0
Comment
Question by:lucavilla
  • 4
  • 4
8 Comments
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40211793
Store the value as a global and then access dClick when you need it
Dim dClick as Date
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
    dClick=DateClicked
End Sub

Open in new window

0
 

Author Comment

by:lucavilla
ID: 40211811
A msgbox(dClick) in another sub display a "00:00:00"  :(
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40211834
Did you activate the click event on MonthView1 before you called the other sub?
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:lucavilla
ID: 40211846
hmm no, how do I do it?
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40211895
click on the MonthView1 component you put on the sheet and select a date
0
 

Author Comment

by:lucavilla
ID: 40211939
Not sure if I understood, I just created a button and assigned to it a macro named MySub that calls the calendar with this command:
UserForm1.Show

After the user selects a date, the calendar sub MonthView1_DateClick ends with an "Unload Me" and all the other instructions after "UserForm1.Show" of MyMacro are executed.
The problem is that I cannot read the date from these following commands in MySub.
I can only read it from inside the MonthView1_DateClick sub.
0
 
LVL 21

Accepted Solution

by:
Randy Poole earned 500 total points
ID: 40212023
Gotcha, add a module to the VBA code, add the following code:
Public dClick as Date

Open in new window


then in your user form:
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
    On Error Resume Next
        Module1.dClick=DateClicked
	MsgBox (DateClicked)
    Unload Me
End Sub

Open in new window

You can now access Module1.dClick from any macro
0
 

Author Closing Comment

by:lucavilla
ID: 40213662
Works wonderfully! Thanks!


In UserForm1 I wrote:
Public Sub MonthView1_DateClick(ByVal dateClicked As Date)
    On Error Resume Next
    dClick = dateClicked
    Unload Me
End Sub


In Module1 I wrote:
Public dClick As Date
Sub Aggiorna_dati_consulenza()
    UserForm1.Show
    MsgBox (dClick)
End Sub



Msgbox correctly show the selected date "17/07/2014"!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question