[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 715
  • Last Modified:

Date Picker Form

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
tracyms
Asked:
tracyms
  • 2
  • 2
1 Solution
 
akbCommented:
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
 
tracymsAuthor Commented:
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
 
akbCommented:
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
 
tracymsAuthor Commented:
Duh... :-). Works great!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now