Solved

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

Posted on 2014-07-22
8
856 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
Comment Utility
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
Comment Utility
A msgbox(dClick) in another sub display a "00:00:00"  :(
0
 
LVL 21

Expert Comment

by:Randy Poole
Comment Utility
Did you activate the click event on MonthView1 before you called the other sub?
0
 

Author Comment

by:lucavilla
Comment Utility
hmm no, how do I do it?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 21

Expert Comment

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

Author Comment

by:lucavilla
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now