Excel将现有日期保留在单元格中(日历VBA)

时间:2018-11-17 21:54:07

标签: excel vba excel-vba date calendar

我正在使用VBA日历在工作簿中输入日期。我已将代码添加到工作表中,以双击单元格来调用日历。因此,基本上,当我双击范围内的单元格时,我会得到日历表格,在其中选择日期,然后将其插入到双击的单元格中。唯一的问题是,在关闭日历表单后,我的单元格被选中,并且在单元格中看到了公式。如何使它关闭日历表单而不在其后选择单元格? 我已经使用了这个日历: https://trevoreyre.com/portfolio/excel-datepicker/

双击范围内的单元格以调用日历表格的代码:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)


  Dim xRg As Object


    If Not Intersect(Target, Range("E7:F187")) Is Nothing Then dateVariable = CalendarForm.GetDate
  if datevariable=0 then
   exit sub
  else
  For Each xRg In Selection.Cells
  xRg.Value = dateVariable
  Next xRg
End if
'datevariable=0


    End Sub

1 个答案:

答案 0 :(得分:1)

尝试一下。看来您可以在代码中选择另一个单元格。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim rng As Range
    Set rng = Target.Offset(1, 0)

    If Not Intersect(Target, Range("E7:F187")) Is Nothing Then datevariable = CalendarForm.GetDate
    If datevariable = 0 Then
        rng.Select
        Exit Sub
    Else
        Target.value = datevariable
        rng.Select
    End If

End Sub