KutoolsforOffice — 一套方案,五大工具。事半功倍。

如何在 Excel 中创建月度或年度日历?

作者修改日期

有时您需要在 Excel 中快速创建特定月份或年份的日历,该如何实现?本教程将为您介绍高效创建月度或年度日历的实用技巧。

通过 Excel 模板创建月度或年度日历

通过 VBA 创建月度日历

使用永久日历轻松创建月度或年度日历


通过 Excel 模板创建月度或年度日历

在 Excel 中,您可以轻松使用日历模板创建月度或年度日历。

1. 在 Excel 2010/2013 中,单击文件> 新建;在 Excel 2007 中,单击 Office 按钮> 新建,然后在弹出窗口右侧的搜索引擎中输入 calendar。请参见截图:

在 Excel 2010/2013 中

在 Excel 中搜索日历模板的截图

在 Excel 2007 中

在 Excel 2007 中搜索日历模板的截图

2. 按 Enter 键,窗口中将列出多种日历类型。选择您需要的日历类型,然后单击右侧窗格中的下载(或创建)按钮。请参见截图:

下载所选日历模板的截图

现已在新工作簿中成功创建日历。请参见下方截图:

已创建的日历的截图


通过 VBA 创建月度日历

有时,您可能需要为特定月份(例如 2015 年 1 月)创建一个月度日历。使用上述方法可能难以找到合适的日历模板。接下来,我将为您介绍一段 VBA 代码,助您轻松生成指定月份的日历。

1. 按 Alt + F11 键打开 Microsoft Visual Basic for Applications 窗口,点击插入> 模块,然后将下方的 VBA 代码复制并粘贴到窗口中。

VBA:轻松创建专属月度日历。

Sub CalendarMaker()
       ' Unprotect sheet if had previous calendar to prevent error.
       ActiveSheet.Protect DrawingObjects:=False, Contents:=False, _
          Scenarios:=False
       ' Prevent screen flashing while drawing calendar.
       Application.ScreenUpdating = False
       ' Set up error trapping.
       On Error GoTo MyErrorTrap
       ' Clear area a1:g14 including any previous calendar.
       Range("a1:g14").Clear
       ' Use InputBox to get desired month and year and set variable
       ' MyInput.
       MyInput = InputBox("Type in Month and year for Calendar ")
       ' Allow user to end macro with Cancel in InputBox.
       If MyInput = "" Then Exit Sub
       ' Get the date value of the beginning of inputted month.
       StartDay = DateValue(MyInput)
       ' Check if valid date but not the first of the month
       ' -- if so, reset StartDay to first day of month.
       If Day(StartDay) <> 1 Then
           StartDay = DateValue(Month(StartDay) & "/1/" & _
               Year(StartDay))
       End If
       ' Prepare cell for Month and Year as fully spelled out.
       Range("a1").NumberFormat = "mmmm yyyy"
       ' Center the Month and Year label across a1:g1 with appropriate
       ' size, height and bolding.
       With Range("a1:g1")
           .HorizontalAlignment = xlCenterAcrossSelection
           .VerticalAlignment = xlCenter
           .Font.Size = 18
           .Font.Bold = True
           .RowHeight = 35
       End With
       ' Prepare a2:g2 for day of week labels with centering, size,
       ' height and bolding.
       With Range("a2:g2")
           .ColumnWidth = 11
           .VerticalAlignment = xlCenter
           .HorizontalAlignment = xlCenter
           .VerticalAlignment = xlCenter
           .Orientation = xlHorizontal
           .Font.Size = 12
           .Font.Bold = True
           .RowHeight = 20
       End With
       ' Put days of week in a2:g2.
       Range("a2") = "Sunday"
       Range("b2") = "Monday"
       Range("c2") = "Tuesday"
       Range("d2") = "Wednesday"
       Range("e2") = "Thursday"
       Range("f2") = "Friday"
       Range("g2") = "Saturday"
       ' Prepare a3:g7 for dates with left/top alignment, size, height
       ' and bolding.
       With Range("a3:g8")
           .HorizontalAlignment = xlRight
           .VerticalAlignment = xlTop
           .Font.Size = 18
           .Font.Bold = True
           .RowHeight = 21
       End With
       ' Put inputted month and year fully spelling out into "a1".
       Range("a1").Value = Application.Text(MyInput, "mmmm yyyy")
       ' Set variable and get which day of the week the month starts.
       DayofWeek = WeekDay(StartDay)
       ' Set variables to identify the year and month as separate
       ' variables.
       CurYear = Year(StartDay)
       CurMonth = Month(StartDay)
       ' Set variable and calculate the first day of the next month.
       FinalDay = DateSerial(CurYear, CurMonth + 1, 1)
       ' Place a "1" in cell position of the first day of the chosen
       ' month based on DayofWeek.
       Select Case DayofWeek
           Case 1
               Range("a3").Value = 1
           Case 2
               Range("b3").Value = 1
           Case 3
               Range("c3").Value = 1
           Case 4
               Range("d3").Value = 1
           Case 5
               Range("e3").Value = 1
           Case 6
               Range("f3").Value = 1
           Case 7
               Range("g3").Value = 1
       End Select
       ' Loop through range a3:g8 incrementing each cell after the "1"
       ' cell.
       For Each cell In Range("a3:g8")
           RowCell = cell.Row
           ColCell = cell.Column
           ' Do if "1" is in first column.
           If cell.Column = 1 And cell.Row = 3 Then
           ' Do if current cell is not in 1st column.
           ElseIf cell.Column <> 1 Then
               If cell.Offset(0, -1).Value >= 1 Then
                   cell.Value = cell.Offset(0, -1).Value + 1
                   ' Stop when the last day of the month has been
                   ' entered.
                   If cell.Value > (FinalDay - StartDay) Then
                       cell.Value = ""
                       ' Exit loop when calendar has correct number of
                       ' days shown.
                       Exit For
                   End If
               End If
           ' Do only if current cell is not in Row 3 and is in Column 1.
           ElseIf cell.Row > 3 And cell.Column = 1 Then
               cell.Value = cell.Offset(-1, 6).Value + 1
               ' Stop when the last day of the month has been entered.
               If cell.Value > (FinalDay - StartDay) Then
                   cell.Value = ""
                   ' Exit loop when calendar has correct number of days
                   ' shown.
                   Exit For
               End If
           End If
       Next

       ' Create Entry cells, format them centered, wrap text, and border
       ' around days.
       For x = 0 To 5
           Range("A4").Offset(x * 2, 0).EntireRow.Insert
           With Range("A4:G4").Offset(x * 2, 0)
               .RowHeight = 65
               .HorizontalAlignment = xlCenter
               .VerticalAlignment = xlTop
               .WrapText = True
               .Font.Size = 10
               .Font.Bold = False
               ' Unlock these cells to be able to enter text later after
               ' sheet is protected.
               .Locked = False
           End With
           ' Put border around the block of dates.
           With Range("A3").Offset(x * 2, 0).Resize(2, _
           7).Borders(xlLeft)
               .Weight = xlThick
               .ColorIndex = xlAutomatic
           End With

           With Range("A3").Offset(x * 2, 0).Resize(2, _
           7).Borders(xlRight)
               .Weight = xlThick
               .ColorIndex = xlAutomatic
           End With
           Range("A3").Offset(x * 2, 0).Resize(2, 7).BorderAround _
              Weight:=xlThick, ColorIndex:=xlAutomatic
       Next
       If Range("A13").Value = "" Then Range("A13").Offset(0, 0) _
          .Resize(2, 8).EntireRow.Delete
       ' Turn off gridlines.
       ActiveWindow.DisplayGridlines = False
       ' Protect sheet to prevent overwriting the dates.
       ActiveSheet.Protect DrawingObjects:=True, Contents:=True, _
          Scenarios:=True

       ' Resize window to show all of calendar (may have to be adjusted
       ' for video configuration).
       ActiveWindow.WindowState = xlMaximized
       ActiveWindow.ScrollRow = 1

       ' Allow screen to redraw with calendar showing.
       Application.ScreenUpdating = True
       ' Prevent going to error trap unless error found by exiting Sub
       ' here.
       Exit Sub
   ' Error causes msgbox to indicate the problem, provides new input box, 
   ' and resumes at the line that caused the error.
   MyErrorTrap:
       MsgBox "You may not have entered your Month and Year correctly." _
           & Chr(13) & "Spell the Month correctly" _
           & " (or use 3 letter abbreviation)" _
           & Chr(13) & "and 4 digits for the Year"
       MyInput = InputBox("Type in Month and year for Calendar")
       If MyInput = "" Then Exit Sub
       Resume
   End Sub

该 VBA 代码来自此网站 https://support.microsoft.com/en-us/kb/150774

2. 按 F5 键或点击运行按钮后,将弹出一个对话框,提示您输入要创建日历的指定月份。请参见截图:

在日历中输入所需创建月份的截图

3. 单击确定,即可在当前工作表中创建 2015 年 1 月的日历。

已创建的日历的截图

但上述方法存在一些局限。例如,若要一次性创建从 1 月到 5 月的日历,您需重复使用上述两种方法各五次。接下来,我将为您介绍一款实用工具,助您快速轻松地解决这一问题。


使用永久日历轻松创建月度或年度日历

Kutools for Excel永久日历工具是其中一款强大实用的功能,可帮助您在 Excel 中一次性快速创建月度或年度日历。

1. 单击 KUTOOLS PLUS > 工作表 > 永久日历

2. 在弹出的对话框中,指定要创建日历的月份范围,然后单击创建。请参见截图:

在永久日历对话框中指定起始和结束月份的截图

随后将创建一个包含五个月度日历工作表的新工作簿。请参见下方截图:

在 Excel 中显示最终创建的月度日历的截图

提示:

若您只想创建某个月份的日历,只需在对话框的“起始”和“结束”文本框中选择相同的月份即可。

单击此处了解有关永久日历的更多信息


最佳办公效率工具

🤖KUTOOLS AI 助手:基于以下内容革新数据分析:智能执行   |  生成代码|  创建自定义公式  |  数据分析及生成图表|  调用 Kutools Functions……
热门功能查找、高亮或标记重复项   |  删除空白行   |  合并列或单元格且不丢失数据   |  不使用公式的四舍五入……
高级 LOOKUP多条件 VLookup  |  多值 VLookup  |   跨多工作表 VLookup   |   模糊查找……
高级下拉列表快速创建下拉列表   |  级联下拉列表   |  多选下拉列表……
列管理器添加指定数量的列|移动列|切换隐藏列的可见性状态|比较区域与列……
特色功能网格聚焦   |  设计视图   |增强编辑栏   | 工作簿和表管理器   |  资源库(自动文本)|  日期提取   |  汇总工作表  |  加密/解密单元格   | 按列表发送邮件   |  超级筛选   |   特殊筛选(筛选粗体单元格/斜体/删除线……) ......
精选 15 工具集12 文本工具添加文本删除特定字符,……)|   50+ 图表 类型甘特图,……)|   40+ 实用公式基于生日计算年龄,……)|   19 插入工具插入二维码从路径插入图片,……)|   12 转换工具小写金额转大写汇率转换,……)|   7 合并和拆分工具高级合并行分割单元格,……)|……更多
在您的首选语言中使用 Kutools – 支持英语、西班牙语、德语、法语、中文及 40+ 种其他语言!

使用 Kutools for Excel 大幅提升您的 Excel 技能,体验前所未有的高效。Kutools for Excel 提供 300 多项高级功能,助您提升生产力、节省时间。立即点击此处,获取您最需要的功能……


Office Tab 为 Office 带来标签式界面,让您的工作更轻松

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中启用标签式编辑和阅读
  • 在同一个窗口的新标签页中打开并创建多个文档,而非在新窗口中。
  • 将您的工作效率提升 50%,每天减少数百次鼠标点击!

所有 Kutools 插件,一个安装程序

Kutools for Office 套件捆绑了适用于 Excel、Word、Outlook 和 PowerPoint 的插件以及 Office Tab Pro,非常适合需要跨多个 Office 应用高效协作的团队。

ExcelWordOutlookTabsPowerPoint
  • 一体化套件— Excel、Word、Outlook 和 PowerPoint 插件 + Office Tab Pro
  • 一个安装程序,一个许可证— 几分钟内完成设置(支持 MSI)
  • 协同效果更佳— 在多个 Office 应用中实现高效协同
  • 30 天全功能试用— 无需注册,无需信用卡
  • 超值之选— 比单独购买插件更省钱