Solved

Date Picker Form

Posted on 2014-04-25
4
688 Views
Last Modified: 2014-04-25
I've tweaked some code for a pop up date picker I found here:

http://www.excel-it.com/excel_datepickers.html

I'd like to have the date picker "remember" the date value in the cell if I double click on it. Currently, I can change the date and it stays in the cell but if double click the cell again, it defaults back to the current date.

In other words, if I change the date and double click the same cell, I want it to show that date. If the cell is blank, then show current date on the form.

Also, I'd like the date in the cells to be mm/dd/yy but still show the month name, month dd, and month yyyy on the actual date form picker as it does now.

I've attached an example file. Thank you.
UserForm-Date-Test.xlsm
0
Comment
Question by:tracyms
  • 2
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
akb earned 500 total points
ID: 40023908
Change the VBA as follows:

Create a variable called xDate

Change each reference to Date to xDate

When the form loads check active cell for blank. If blank load xDate with today's date. Otherwise load xDate with the data from the active cell.


Full code:


Option Explicit
Dim i As Long
Dim L As Long
Dim xDate As Date



Private Sub cboMonth_Change()

End Sub

Private Sub CommandButton1_Click()
'you can use short date or a custom date formatas an alternative
  ActiveCell.Value = Format(DateSerial(CLng(Me.cboYear.Value), Me.cboMonth.ListIndex + 1, Me.cboDay.Value), "short date")
End Sub

Private Sub UserForm_Initialize()
If ActiveCell.Value = "" Then
  xDate = Date
Else
  xDate = ActiveCell.Value
End If


With Me
.lblDate.Caption = "Select date:"
  With .cboYear
            .Clear
            'change the number to adjust years shown
            For i = -1 To 1
                .AddItem (Format(xDate, "YYYY")) + i
            Next
            .ListIndex = 1
        End With
        With .cboMonth
            .Clear
            .List = Application.GetCustomListContents(4)
            .ListIndex = Month(xDate) - 1
            L = .ListIndex
        End With
End With

loadDays

End Sub
Sub loadDays()
    i = Me.cboMonth.ListIndex + 1

    With Me.cboDay
        .Clear
        Select Case i
            Case 4, 6, 9, 11
                L = 30
            Case 2
                If LeapYear(Me.cboYear.Value) Then
                    L = 29
                Else: L = 28
                End If
            Case Else
                L = 31
        End Select
        For i = 1 To L
            .AddItem Format(i, "00")
        Next i
                    .ListIndex = Day(xDate) - 1
    End With
End Sub
UPDATED-UserForm-Date-Test.xlsm
0
 

Author Comment

by:tracyms
ID: 40023962
This looks to be working, the only missing part is the date format in the cell per my posted request:

"Also, I'd like the date in the cells to be mm/dd/yy but still show the month name, month dd, and month yyyy on the actual date form picker as it does now."

Is it possible for the dates in the cell to be mm/dd/yy? Thanks!
0
 
LVL 13

Expert Comment

by:akb
ID: 40023967
The date format usually matches that of your region settings. You can override this by custom formatting the cells in the spreadsheet.

Highlight the cells you want to format
Right click
Select Format Cells...
Under the Number tab select Custom
In the Type field type mm/dd/yyyy
Click OK
0
 

Author Comment

by:tracyms
ID: 40023974
Duh... :-). Works great!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 how to use a scrolling table in Microsoft Excel using the INDEX function.

920 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

15 Experts available now in Live!

Get 1:1 Help Now