Solved

Date Picker Form

Posted on 2014-04-25
4
704 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
[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
  • 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
 
LVL 1

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
 
LVL 1

Author Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Introduction to Processes

724 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