【问题标题】:Excel leave existing date in cell (calendar VBA)Excel在单元格中保留现有日期(日历VBA)
【发布时间】:2019-04-20 16:58:43
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: excel vba date calendar


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-18
      相关资源
      最近更新 更多