Excel VBA 通过表单中的命令按钮调用日历

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5942391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:11:56  来源:igfitidea点击:

Excel VBA Calling the Calendar via Command Button in a form

excelvbabuttoncalendarcommand

提问by bdubb

I have just added in Calendar 12.0 from the tools > Additional Controls. Calendar works fine and I have it spitting the date out to the right cells. What I would like, however, is to really make the calendar visible from a command button as my form contains a bunch of fields and I don't want to bog up the form with this calendar. I have tried Calendar1.show but the .show isn't an option.

我刚刚从工具 > 附加控件中添加了 Calendar 12.0。日历工作正常,我让它将日期吐出到正确的单元格。然而,我想要的是真正让日历从命令按钮可见,因为我的表单包含一堆字段,我不想用这个日历使表单陷入困境。我试过 Calendar1.show 但 .show 不是一个选项。

So ultimately I need a command button to show the calendar, allow the user to select (I have that) and then close the calendar. Any help? I thank you in advance!!

所以最终我需要一个命令按钮来显示日历,允许用户选择(我有)然后关闭日历。有什么帮助吗?我提前谢谢你!!

bdubb

配音

回答by Duncan Howe

In this snippet, CommandButton1 is from the ActiveX controls, not the form controls. It requires that you click the button to show the calendar (which pops up next to the button you clicked), and click the button again to hide the calendar.

在此代码段中,CommandButton1 来自 ActiveX 控件,而不是表单控件。它要求您单击该按钮以显示日历(它会在您单击的按钮旁边弹出),然后再次单击该按钮以隐藏日历。

Private Sub CommandButton1_Click()

If Not Calendar1.Visible Then
    Calendar1.LinkedCell = "A1"
    Calendar1.Top = Sheet1.CommandButton1.Top
    Calendar1.Left = Sheet1.CommandButton1.Left + Sheet1.CommandButton1.Width + 1
    Calendar1.Visible = True
Else
    Calendar1.Visible = False
End If

End Sub

Obviously, different buttons would require different linked cells, but it does mean that you could have a single calendar control that it displyed by multiple buttons (if that is what you want).

显然,不同的按钮需要不同的链接单元格,但这确实意味着您可以拥有一个由多个按钮显示的日历控件(如果这是您想要的)。

Unfortunately, it would appear that you cannot hide the control while any of its events are firing (e.g AfterUpdate). It just doesn't want to disappear!!

不幸的是,当控件的任何事件正在触发时(例如 AfterUpdate),您似乎无法隐藏控件。它只是不想消失!!

回答by Tryphon

Hide/Close a calendar control still not works (new year 2015 = almost four years later) but I think I found a workaround to hide the control after firing events.

隐藏/关闭日历控件仍然无效(2015 年新年 = 将近四年后),但我想我找到了一种在触发事件后隐藏控件的解决方法。

I have a Calendar1_AfterUpdate(), which launches before Calendar1_Click(). Code is placed directly in a worksheet and NOT in a module.

我有一个 Calendar1_AfterUpdate(),它在 Calendar1_Click() 之前启动。代码直接放在工作表中,而不是放在模块中。

Private Sub Calendar1_AfterUpdate()
  Range("a1") = Me.Calendar1.Value
  ' Next two lines does not work within AfterUpdate
  ' When running step by step it seems to work but the control is
  ' visible when End Sub has run
  Me.Calendar1.Visible = True
  Me.Calendar1.Visible = False
End Sub

To that I simply added

对此我只是补充说

Private Sub Calendar1_Click()
  Me.Calendar1.Visible = True
  Me.Calendar1.Visible = False
End Sub

Please note that the control for some reason must be made visible before hiding.

请注意,出于某种原因,必须在隐藏之前使控件可见。

Why it does not work directly in Calendar1_AfterUpdate() is a mystery to me.

为什么它不能直接在 Calendar1_AfterUpdate() 中工作对我来说是个谜。

Next problem is to hide the control when I remove the mouse. Mouse-events seems to be impossible in a calendar control ...

下一个问题是当我移除鼠标时隐藏控件。鼠标事件在日历控件中似乎是不可能的......