首先我们找到开发工具栏下的ActiveX控件——其他控件,如图所示。
然后找到我们需要的控件
最后用代码控制DTP控件的位置和输入。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Me.DTPicker1
If Target.Address = Range("a2").Address Then
.Visible = True
.Top = Selection.Top
.Left = Selection.Left
.Height = Selection.Height
.Width = Selection.Width
If Target.Cells(1, 1) <> "" Then
.Value = Target.Cells(1, 1).Value
Else
.Value = Date
End If
Else
.Visible = False
End If
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("a2").Address Then
If Target.Cells(1, 1).Value = "" Then
Me.DTPicker1.Visible = False
End If
End If
End Sub
Private Sub DTPicker1_CloseUp()
ActiveCell.Value = Me.DTPicker1.Value
Me.DTPicker1.Visible = False
End Sub