Solved

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

Posted on 2014-07-22
8
943 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

791 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