Populate a cell with a date using DatePicker

John1953

New Member
Joined
Aug 28, 2019
Messages
22
Office Version
  1. 365
Platform
  1. Windows
When I select a cell to add a date using DatePicker drop down box, I select a date but nothing displays in the selected cell. The code I used is;

Private Sub Worksheet_SelectionChange(ByVal Target As Range)


With Sheet1.DTPicker1
.Height = 20
.Width = 20
If Not Intersect(Target, Range("A:A")) Is Nothing Then
.Visible = True
.Top = Target.Top
.Left = Target.Offset(0, 1).Left
Linkedcell = Target.Address
Else
.Visible = False
End If
End With
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi John, welcome to the forum.

Try this

- Select cell on column A
- Select Date
- Or press Esc or any cell to hide dtpicker
- Or change date in the box and press Enter

Code:
Private Sub DTPicker1_Change()
  With Sheet1.DTPicker1
    ActiveCell.Value = .Value
    .Visible = False
    ActiveCell.Activate
  End With
End Sub


Private Sub DTPicker1_KeyPress(KeyAscii As Integer)
  If KeyAscii = 13 Then
    With Sheet1.DTPicker1
      ActiveCell.Value = .Value
      .Visible = False
      ActiveCell.Activate
    End With
  End If
End Sub


Private Sub DTPicker1_LostFocus()
  Sheet1.DTPicker1.Visible = False
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Not Intersect(Target, Range("A:A")) Is Nothing Then
    With Sheet1.DTPicker1
      .Visible = True
      .Top = Target.Top
      .Left = Target.Offset(0, 1).Left
    End With
  Else
    Sheet1.DTPicker1.Visible = False
  End If
End Sub
 
Last edited:
Upvote 0
I can't answer your private message because your mailbox is full
 
Upvote 0
DanteAmor

I entered your code as shown and it works great, however, if I select a row (row number on the left) I get the following message;
Run-Time error '1004';
Application-Defined or Object-Defined error

Any ideas?
 
Upvote 0
DanteAmor

I entered your code as shown and it works great, however, if I select a row (row number on the left) I get the following message;
Run-Time error '1004';
Application-Defined or Object-Defined error

Any ideas?

Try this, add line in red.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
[COLOR=#ff0000]if target.count > 1 then exit sub[/COLOR]
  If Not Intersect(Target, Range("A:A")) Is Nothing Then
    With Sheet1.DTPicker1
      .Visible = True
      .Top = Target.Top
      .Left = Target.Offset(0, 1).Left
    End With
  Else
    Sheet1.DTPicker1.Visible = False
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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