Excel Vba 中的日期选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12303441/
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 17:36:43  来源:igfitidea点击:

DatePicker in Excel Vba

excelvba

提问by P. Sohm

I need to add a datepicker in Excel in a custom vba form.

我需要在 Excel 中以自定义 vba 形式添加日期选择器。

I know that I can use MSCOMCT2.ocx but it seems to be obsolete with Excel 2010 : user will have to install a custom package.

我知道我可以使用 MSCOMCT2.ocx,但它似乎在 Excel 2010 中已过时:用户必须安装自定义包。

I need that the code will work from Excel 2003 to 2010 without installation (or at least without throwing an error message if the component isn't available). I can't control the running environnement.

我需要代码无需安装即可从 Excel 2003 运行到 2010(或者如果组件不可用,至少不会抛出错误消息)。我无法控制运行环境。

IS there any solution ? like a custom datepicker in full vba ?

有什么解决办法吗?像完整的 vba 中的自定义日期选择器?

回答by Michael

I've used the following successfully on a roll-out to a mix of Excel 2003 and 2007. The authors do say it should be compatible with all versions.

我已经成功地将以下内容用于 Excel 2003 和 2007 的混合。作者确实说它应该与所有版本兼容。

https://sites.google.com/site/e90e50/calendar-control-class

https://sites.google.com/site/e90e50/calendar-control-class

回答by Ryan Nguyen

Instead of date picker, i am using auto populate in all the date column in my excel. Please see below.

我没有使用日期选择器,而是在我的 excel 的所有日期列中使用自动填充。请参阅下文。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRg As Range, xCell As Range
On Error Resume Next
If (Target.Count = 1) Then
    If (Not Application.Intersect(Target, Me.Range("H4,H7")) Is Nothing) Then _
        Target.Offset(0, x1down) = Date
    Application.EnableEvents = False
    Set xRg = Application.Intersect(Target.Dependents, Me.Range("H4,H7"))
    If (Not xRg Is Nothing) Then
        For Each xCell In xRg
            xCell.Offset(0, x1down) = Date
                        Next
    End If
    Application.EnableEvents = True
End If
End Sub