Solved

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

Posted on 2014-07-22
8
1,012 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

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.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

688 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