Userform same Calendar used with 2 Userform textboxes

LNG2013

Active Member
Joined
May 23, 2011
Messages
466
I have 2 textboxes DataBox1 and DataBox2, I have them both configured that when they are clicked a calendar pops up for the user to select the date. However, I can't figure out how to make it the Calendar fill the slected TextBox, right now I have it working for DataBox1 but can't figure out how to make it work for 1 & 2.

Code:
Option Explicit
'Thanks to fontstuff for the example code
' ===================================================
' Code by Martin Green    eMail [EMAIL="martin@fontstuff.com"]martin@fontstuff.com[/EMAIL]
' Visit my Office Tips web site at [URL="http://www.fontstuff.com"]www.fontstuff.com[/URL]
' ===================================================
Private Sub cmdClose_Click()
' Close the UserForm
    Unload Me
End Sub
Private Sub UserForm_Initialize()
' Check if active cell contains a date. If 'yes' show
' same date on calendar. If 'no' show today's date.
    If IsDate(DataReport.DateBox1.Value) Then
        Calendar1.Value = DateValue(DataReport.DateBox1.Value)
    Else
        Calendar1.Value = Date
    End If
End Sub
Private Sub Calendar1_Click()
' Transfer date selected on calendar to active cell
' and close UserForm.
    DataReport.DateBox1.Value = Calendar1.Value
    Unload Me
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Which version of Excel are you using?

I have just tried this and it works OK.

Created userform with Calendar Control.
Add 2 textboxes named them txtStart and txtEnd
Added a command button named it cmdDates.

Double click the cmdDates button and added the following code.

Private Sub cmdDates_Click()
If Me.cmdDates.Caption = "Start Date" Then
Me.txtStart = Me.Calendar1.Value
Me.cmdDates.Caption = "End Date"
ElseIf Me.cmdDates.Caption = "End Date" Then
Me.txtEnd = Me.Calendar1.Value
Me.cmdDates.Caption = "Start Date"
End If

End Sub
 
Upvote 0
What code are you using to show the UserForm? If you Hide the UserForm instead of Unloading it, the calling procedures will have access to the Calendar's current value.
 
Upvote 0
I am using Excel 2003,

Here is the rest of the code for the DataReport that is the form the TextBoxes are on.
Trevor - is there a way to do this without an additional button. I just wanted the Users to click the textboxt, calendar pops up, they select their date in each one then run the report.

Code:
Private Sub UserForm_Initialize()
 
        Me.GoalBox1.Enabled = False
        Me.ObjBox1.Enabled = False
        Me.RunButton.Enabled = False
 
    With DateBox1
        .Text = "mm/dd/yyyy"
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
        With DateBox2
        .Text = "mm/dd/yyyy"
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
End Sub
Private Sub DateBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
DateBox1.Text = ""
    frmCalendar.Show
End Sub
Private Sub DateBox2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
DateBox2.Text = ""
    frmCalendar.Show
End Sub
Private Sub ComboBox1_Change()
    If Objective5m.FilterMode = True Then: Objective5m.ShowAllData
    Objective5m.Range("A1").AutoFilter Field:=14, Criteria1:="=" & DataReport.ComboBox1.Value
 
    ' ~~~ This fills the GoalBox with the possible Goals
        DataReport.GoalBox1.Clear
    Call FillCombobox(Objective5m.Range("P2", Objective5m.Cells(Rows.Count, "P").End(xlUp)).SpecialCells(xlCellTypeVisible), DataReport.GoalBox1)
 
    Me.GoalBox1.Enabled = True
 
End Sub
 
Private Sub GoalBox1_Change()
    DataReport.ObjBox1.Clear
    Objective5m.Range("A1").AutoFilter Field:=16, Criteria1:="=" & DataReport.GoalBox1.Value
    Call FillCombobox(Objective5m.Range("Q2", Objective5m.Cells(Rows.Count, "Q").End(xlUp)).SpecialCells(xlCellTypeVisible), DataReport.ObjBox1)
 
    Me.ObjBox1.Enabled = True
    Me.RunButton.Enabled = True
End Sub
Private Sub ObjBox1_Change()
    Objective5m.Range("A1").AutoFilter Field:=17, Criteria1:="=" & DataReport.ObjBox1.Value
End Sub
Private Sub RunButton_Click()
Unload DataReport
'
' This code adds the ability to select the month range for the autofilter
' Field:=24 refers to the 24th column
 
Rows("1").AutoFilter Field:=9, _
                     Criteria1:=">=" & Me.DateBox1, _
                     Operator:=xlAnd, _
                     Criteria2:="<=" & Me.DateBox2
Call Filtered
Call CheckForNoData
End Sub
Private Sub CancelButton_Click()
Unload DataReport
End Sub
 
Upvote 0
Example:

Code:
'UserForm DataReport
 
Private Sub DateBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    frmCalendar.Show
    DateBox1.Value = frmCalendar.Calendar1.Value
    Unload frmCalendar
End Sub
 
Private Sub DateBox2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    frmCalendar.Show
    DateBox2.Value = frmCalendar.Calendar1.Value
    Unload frmCalendar
End Sub
 
'UserForm frmCalendar
 
Private Sub Calendar1_Click()
    Me.Hide
End Sub
 
Upvote 0
That worked like a charm Andrew thank you very much. Iwas wracking my brain on how to do this, and began thinking I was going to have to assign each one of the DateBoxes as a variable.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top