KutoolsforOffice — 一套方案,五大工具。事半功倍。三月特賣:20% 折扣

如何在 Excel 中建立月曆或年曆?

作者Sun修改日期

有時您需要在 Excel 中快速建立特定月份或年份的日曆,該如何輕鬆完成呢?本教學將介紹幾種實用技巧,協助您迅速打造專屬的月曆或年曆。

透過 Excel 範本建立月曆或年曆

使用 VBA 建立月曆

輕鬆透過永久日曆建立月曆或年曆


透過 Excel 範本建立月曆或年曆

在 Excel 中,您可輕鬆運用日曆範本打造專屬的月曆或年曆。

在 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 中最終建立之月份行事曆的螢幕截圖

提示:

若您只想建立特定月份的日曆,只需在對話方塊的「從」與「到」文字框中選擇相同的月份即可。

按此處進一步了解永久日曆


最佳 Office 生產力工具

🤖KUTOOLS AI 助手:基於以下內容徹底革新數據分析:智慧執行     產生程式碼  建立自訂公式    分析資料並產生圖表  呼叫增強函數……
熱門功能尋找、醒目提示或標記重複值     刪除空白行     合併列或儲存格而不遺失資料     不使用公式的四捨五入……
高級 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 中啟用分頁式編輯與閱讀功能,以及 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 天完整功能試用— 無需註冊,無需信用卡
  • 超值之選— 比單獨購買各增益集更省費用