Solved

Date Picker Form

Posted on 2014-04-25
4
681 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Duh... :-). Works great!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

10 Experts available now in Live!

Get 1:1 Help Now