跳到主要内容

单击Excel中的特定单元格时如何弹出日历?

添加一名作者 最后修改时间:2021-01-27

假设工作表中有一个列范围,您需要经常在其中输入和更改日期,但是每次手动输入或更改日期很无聊。 如何快速输入日期而无需手动输入列范围? 本文讨论的是单击某个范围内的单元格时弹出日历,然后在日历中选择日期后将日期自动插入到选定的单元格中。


单击带有VBA代码的特定单元格时弹出日历

请按以下步骤解决此问题。

备注注意:此方法仅适用于32位Microsoft Excel。

步骤1:创建带有日历的用户窗体

请创建一个用户窗体,其中包含您将通过单击单元格弹出的日历。

1。 按 其他 + F11 同时打开 Microsoft Visual Basic应用程序 窗口。

2.在 Microsoft Visual Basic应用程序 窗口中,单击 插页 > 用户表格.

3.然后 用户表格工具箱 窗口弹出 工具箱,单击任意一个控件,然后右键单击,然后选择 附加控制 从右键单击菜单中。 看截图:

4.在 附加控制 对话框中,向下滚动以检查 Microsoft MonthView控件 在选项 可用控件 框,然后单击 OK 按钮。

5.然后您可以看到 月视图 按钮添加到 工具箱 窗口。 请单击此MonthView按钮,然后在UserForm1窗口上单击以在用户窗体中创建日历。

备注:您可以通过拖动用户窗体的边框来调整用户窗体窗口的大小以适合插入的日历。

6.双击UserForm1中的插入日历,然后在 代码 窗口,请用以下VBA脚本替换原始代码。

VBA code: create a user form with calendar

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
 On Error Resume Next
   Dim xRg As Object
   For Each xRg In Selection.Cells
      xRg.Value = DateClicked
   Next xRg 
   Unload Me
End Sub

备注:此代码可帮助在从日历中选择日期后将日期插入到选定的单元格中。

步骤2:点击单元格时激活日历

现在,您需要指定某些单元格才能在单击时弹出日历。 请执行以下操作。

7.双击包含要单击的单元格的工作表名称,以在左侧弹出日历 项目 窗格,然后将下面的VBA代码复制并粘贴到“代码”窗口中。 看截图:

VBA code: Click cell to pop up calendar

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If (Target.Count = 1) Then
    If Not Intersect(Target, Range("A2:A10")) Is Nothing Then UserForm1.Show
    End If
End Sub

备注:在代码中,A2:A10是您要单击以弹出日历的单元格。 请根据需要更改单元格范围。

8。 按 其他 + Q 同时关闭按键 Microsoft Visual Basic应用程序 窗口。

从现在开始,单击当前工作表中指定范围内的任何单元格,将弹出一个日历,如下图所示。 从日历中选择日期后,日期会自动插入到选定的单元格中。


单击带有VBA代码的特定单元格时弹出日历

本节介绍 日期选择器 实用程序 Kutools for Excel。 启用此功能后,单击日期单元格将弹出日历,您可以轻松地用新日期替换现有日期。 请按照以下步骤应用此功能。

1。 点击 库工具 > 内容 > 启用日期选择器.

2.启用此功能后,单击日期单元格,日历图标将在该单元格的右侧弹出。

3.单击日历图标以打开 日期选择器 对话框,然后单击一个新日期以替换所选单元格中的日期。

笔记:

  • 撤消按钮:单击此按钮可以撤消替换日期;
  • 关闭按钮:单击此按钮可关闭“日期选择器”对话框;
  • 此功能仅适用于包含日期的单元格。

  如果您想免费试用(30天)此实用程序, 请点击下载,然后按照上述步骤进行操作。


相关文章:

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (18)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I am not seeing the Microsoft MonthView Control in the listing, but it may be as Pete (#32847) mentioned that this will not work in Office 365 or 64-bit Microsoft Office.
This comment was minimized by the moderator on the site
Hi Teagan Caudle,
I searched and tried the methods suggested in google, but ultimately could not register the MonthView control in Microsoft 365.
Sorry for the inconvenience. Maybe you can check this out.
https://social.technet.microsoft.com/Forums/Azure/en-US/db3b4dff-aad7-4d88-87cc-8f3f117be550/microsoft-windows-common-controls-60-for-office-2016
This comment was minimized by the moderator on the site
There is no Microsoft MonthView Control listed (Office 365), and the directions here doesn't explain how one would get that control, so this is pretty much useless unless you're using an older version of Excel.
This comment was minimized by the moderator on the site
The calendar will show up but when I click on the date, the cell doesn't populate
This comment was minimized by the moderator on the site
Hi,
The code works well in my case. Which Excel version are you using?
This comment was minimized by the moderator on the site
Thank you so much! These directions were super useful :)
This comment was minimized by the moderator on the site
Hi, Is it possible to put the date picker pop-up for multiple column, as in my sheet I have "start date", "end date" and "agreement date". if yes then how?
This comment was minimized by the moderator on the site
Hi jeet,
Follow the steps and replace the range "A2:A10" in the second VBA code with your column range (such as C2:E2).
This comment was minimized by the moderator on the site
Salve il codice funziona benissimo, ma se volessi farlo funzionare anche su un altro foglio
This comment was minimized by the moderator on the site
if i try to select a row, the pop up will activate and the date appears in each cell in that row


how can i avoid this
This comment was minimized by the moderator on the site
Hi Sam,
The code has been updated in the article with the problem solving. Please have a try and thank you for your comment.
This comment was minimized by the moderator on the site
tarihi seçebiliyorum ama a1:a10 hücrelerine seçtiğim tarih eklenmiyor. teşekkür ederim
This comment was minimized by the moderator on the site
Hello everyone,

Can anyone tell me how to popup a calendar in a range of cells, but starting only from the cell right bellow a table header and down bellow in an excel column.


Thank you in advance.
This comment was minimized by the moderator on the site
Use i.e: Range("B6:C30")
This comment was minimized by the moderator on the site
Good Day,
Sorry I didn't got your question. Would be nice if you could provide screenshot of what you are trying to do.
This comment was minimized by the moderator on the site
Use i.e: Range("B6:C30")
This comment was minimized by the moderator on the site
i used these VBA codes and everthing's fine so far. The range is A2:A10 and calendar pops up when you select a cell into it. But if you mark row from 2 to 10 again the calendar pops again. It's the same with column "A" if you mark it, again the calendar pops. How should i proceed, in order to get the calendar only in the range i've defined?
There are no comments posted here yet
Load More
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations