如何在Excel中创建月历/年历?
有时,您需要在Excel中创建特定月份或年份的日历,如何快速解决这个问题呢?本教程介绍了在Excel中快速创建月历或年历的技巧。
通过Excel模板创建月历或年历
在Excel中,您可以使用日历模板来创建月历或年历。
1. 在Excel 2010/2013中,点击 文件 > 新建,在Excel 2007中,点击 Office按钮 > 新建,然后在弹出窗口的右侧部分,在搜索引擎中输入“calendar”。参见截图:
在Excel 2010/2013中
在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. 在弹出的对话框中,指定要创建日历的月份范围,并点击 创建。参见截图:
然后,一个新的工作簿被创建,其中包含五个日历工作表。参见截图:
提示:
如果您只想创建特定月份的日历,只需在对话框的“从”和“到”文本框中选择相同的月份即可。
最佳 Office 办公效率工具
🤖 | Kutools AI 助手:基于智能执行,彻底革新数据分析 |生成代码|创建自定义公式|分析数据并生成图表|调用 Kutools Functions… |
热门功能:查找、选中项的背景色或标记重复项|删除空行|合并列或单元格且不丢失数据|四舍五入(无公式)... | |
高级 LOOKUP:多条件查找 (VLookup)|多值查找 (VLookup)|多表查找 (VLookup Across Multiple Sheets)|模糊查找 (Fuzzy Lookup)... | |
高级下拉列表:快速创建下拉列表|依赖型下拉列表|多选下拉列表... | |
列管理器:添加指定数量的列 |移动列 |切换隐藏列的可见状态| 比较区域及列... | |
特色功能:网格聚焦|设计视图|增强编辑栏|工作簿 & 工作表管理器|资源库(自动文本)|日期提取|合并数据|加密/解密单元格|按列表发送电子邮件|超级筛选|特殊筛选(筛选粗体/倾斜/删除线等)... | |
热门15 大工具集:12 款文本工具(添加文本、删除特定字符等)|50+ 种图表 类型(甘特图等)|40+ 实用公式(基于生日计算年龄等)|19 款插入工具(插入二维码、按路径插入图片等)|12 种转换工具(小写金额转大写、汇率转换等)|7 款合并与分割工具(高级合并行、分割单元格等)|...更多精彩等你发现 |
用 Kutools for Excel 加速你的 Excel 技能,体验前所未有的高效办公。 Kutools for Excel 提供300 多项高级功能,助您提升效率,节省大量时间。点击此处,获取你最需要的功能...
Office Tab 为 Office 带来标签式界面,让你的工作更加轻松
- 在 Word、Excel、PowerPoint 启用标签式编辑和阅读
- 在同一窗口的新标签中打开和创建多个文档,无需新建窗口。
- 办公效率提升50%,每天帮你减少上百次鼠标点击!