How to Create Calendar Using VBA in Excel (with Easy Steps)

Get FREE Advanced Excel Exercises with Solutions!

If you want to create an Excel VBA calendar, you have come to the right place. Here, we will walk you through some easy and effective steps to do the task smoothly.


Download Practice Workbook

You can download the Excel file and practice while reading this article.


10 Easy Steps to Create a Calendar Using VBA in Excel

In the following article, we will describe 10 easy and effective steps to create an Excel VBA calendar. You can see the overview of our created calendar. Next, we will describe the steps to create such a calendar. Here, we used Excel 365. You can use any available Excel version.

Excel VBA Calendar


Step-1: Preparing an Outline of Calendar by Using UserForm of VBA in Excel

In this step, we will prepare the outer template of the Calendar using UserForm. This is the primary step for Excel VBA Calendar.

  •  First of all, go to the Developer tab >> select Visual Basic.
  • This will bring out the VBA Editor window.
  • Here, you can also press ALT + F11 keys to bring out the VBA Editor window.

Using Developer Tab to Create Excel VBA Calendar

  • At this moment, a VBA Editor window will appear.
  • After that, from the Insert tab >> select UserForm.

Inserting UserForm to Create Excel VBA Calendar

  • Therefore, you can see the UserForm.
  • Next, we will right-click on the UserForm >> select Properties.

Right-Click on properties

  • Then, a Properties window will appear on the right side of the UserForm.
  • Furthermore, we will set the Name as Calendar.
  • Here, you can set any Name, however, since we are making a calendar, we put the name Calendar.
  • In addition, click on the drop-down arrow of the BackColor box.
  • Then, from Palette >> select White.

Chose color for calendarAlso, set the Caption as Calendar.

  • Moreover, we click on the drop-down arrow of the SpecialEffect box.
  • We select 3-fmSpecialEffectEtched.

  • As a result, the outer template of the Excel VBA Calendar is ready.

Created Outline for Excel VBA Calendar

Read More: How to Make a Calendar in Excel Without Template (2 Examples)


Step-2: Inserting Labels into Our Created Outline of Calendar

In this step, we will insert labels for months and years into the Excel VBA calendar.

  •  In the first place, click on the Toolbox.

Inserting Toolbox to Create Excel VBA Calendar

  • Then, select a Label from the Toolbox >> draw the label in the Calendar.

  • Now, we have named the label Month.
  • Furthermore, we will right-click on Month >> select Properties.

  • After that, in the Properties window, click on the drop-down arrow of the BorderColor box.
  • Then, select 0-fmBackstyleTransparent.

  • In a similar way, we inserted the Year label.
  • Therefore, you can see two labels.

Inserting Month and Year Labels to Create Excel VBA Calendar

Read More: How to Create a Yearly Calendar in Excel (with Easy Steps)


Step-3: Adding ComboBoxes

In this step, we will add two combo boxes for the Month and Year to Excel VBA Calendar.

  • First of all, we will select a ComboBox from the Toolbox.
  • Then, we will draw the ComboBox beside the Month.

Adding ComboBoxto Create Excel VBA Calendar

  • Furthermore, we will right-click on the ComboBox >> select Properties.

  • Then, in the Properties window >> we will set the Name as Cmb_Month.

Giving Name to ComboBox to Create Excel VBA Calendar

  • In addition, we will select 3-fmSpecialEfectEtched as the SpecialEffect.
  • And, we will select 2-fmStyleDropDownList as the Style.

  • Now, we will copy the ComboBox by right-clicking on it and selecting Copy.

  • After that, we place the copied ComboBox bestie the Year label.
  • Furthermore, we will right-click on the ComboBox >> select Properties.

Inserting ComboBox for Year to Create Excel VBA Calendar

  • Then, a Properties window will appear.
  • Afterward, we set the Name as Cmb_Year.

  • At this moment, we will select all the labels and ComboBoxes by pressing the CTRL key.
  • After that, we will right-click on them.
  • Then, from the Align group >> we will select Middles.

Aligning ComboBoxes and Lebels in Center to Create Excel VBA Calendar

  • As a result, you can see the Comboboxes for Month and Year.

Read More: How to Create a Monthly Calendar in Excel (3 Effective Ways)


Step-4: Writing Codes for ComboBoxes

In this step, we will write the codes for the ComboBoxes to show the Months and Years.

  •  In the beginning, we will double-click anywhere on the Calendar.

  • Then, a Module to write code will appear.
  • Furthermore, we will type the following code.
Private Sub UserForm_Initialize()
Dim C_Month As Integer

For C_Month = 1 To 12
Me.Cmb_Month.AddItem VBA.Format(VBA.DateSerial(2020, C_Month, 1), "MMMM")
Next C_Month
Me.Cmb_Month.Value = VBA.Format(VBA.Date, "MMMM")

For C_Month = VBA.Year(Date) - 20 To VBA.Year(Date) + 20
Me.Cmb_Year.AddItem C_Month
Next C_Month
Me.Cmb_Year.Value = VBA.Format(VBA.Date, "YYYY")

End Sub

Inserting Codes to Create Excel VBA Calendar

  • Later, Save the code.
  • Next, before running the code, we will return to the Calendar, and select the ComandBoxes by pressing the CTRL key.
  • Then, we will right-click >> select Properties.

After that, we will select 20 for ListRows.

  • This will display all the months at once and we do not need to scroll down to see all the months.

Adjusting ComboBoxes List Rows to Create Excel VBA Calendar

  • After that, we will Run the code.

  • Therefore, you can see the current month will appear.
  • You can click on the drop-down arrow and select any other month.

  • In addition, you can see the current Year.
  • You can click on the drop-down arrow and select any other year.
  • Hence, we have created the months and years in the Excel VBA calendar.

Read More: How to Create an Event Calendar in Excel (with Easy Steps)


Step-5: Inserting Images to Calendar Outline

In this step, we will insert images into our Excel VBA calendar.

We will insert a background image, a right arrow, and a left arrow so that we can easily move toward the right and left.

  • First of all, we will select the Image from the ToolBox.
  • Then, we will draw the image box.

Inserting Image to Create Excel VBA Calendar

  • Next, we will right-click on the image box >> select Properties.

  • Then, we will select 0-fmBorderStyleNone as the BorderStyle.
  • Further, we will select a Picture.

  • Here, we have three pictures in a specific folder.
  • You can use any pictures you want.
Note: Your pictures must be in .jpg format. Also, you must resize your picture according to your needs, otherwise, the picture defaults to a big size which will be inconvenient to use in the Calendar.

Keeping Images in a Folder to Create Excel VBA Calendar

  • Moreover, in the Picture box >> we will click on the three dots to insert an image.

  • Then, we selected the Back Ground image >> click Open.

  • Therefore, you can see the Picture in the Image box.

Inserting Back Ground Image to Create an Excel VBA Calendar

  • In addition, in the Properties box, we will select True as AutoSize.
  • This will make the picture size suitable.

  • In addition, we add a Right arrow and a Left arrow.
  • Next, we will click on the Left arrow.

Employing Left and Right Arrows to Create an Excel VBA Calendar

  • This will open a Properties box.
  • Then, we will set the Name as Previous_Month.
  • In addition, we will set the ControlTipText as Previous_Month.

Providing Names for Left Arrow to Create an Excel VBA Calendar

After that, we will click on the Left arrow.

  • This will open a Properties box.
  • Then, we will set the Name as Next_Month.
  • In addition, we will set the ControlTipText as Next_Month.

Providing Names for Right Arrow to Create an Excel VBA Calendar


Step-6: Adding Month and Day Names in Calendar Outline

In this step, we will add the month and day names to the Excel VBA calendar.

  • First, to add the month name, we will copy the Month label.

  • Then, we will paste the Month into the calendar.
  • We will right-click on the Month >> select Properties.

  • Then, in the Properties window, we will set the Name as Month_Name.
  • Further, we will select a White Fore Color.
  • In addition, we will click on the Font box.

Inserting Month Name Box to Create an Excel VBA Calendar

  • Then, a Font window will appear.
  • Afterward, we will select Bold as the Font Style >> select Size 9 >> click OK.

  • In addition, we will select Center text alignment.

  • Furthermore, we will place the Month in between the Left and Right arrows.
  • Next, we will add another image where we will put all the day names.
  • Therefore, from the Toolbox >> we select Image.
  • We place the Image like the following picture.

  • Then, we copy the Month label.
  • See the following picture.

  • Furthermore, we place the Month in the image box >> edit the name as Sun to indicate Sunday.
  • In addition, we right-click on the Sun >> select Properties.

Adding Day Name to Create an Excel VBA Calendar

  • Then, in the Properties box >> select Center as TextAlign.
  • In addition, click on the Font box.

  • Then, a Font window will appear.
  • Afterward, we will select Bold as the Font Style >> select Size 9 >> click OK.

  • Furthermore, we have added other days of the week.
  • Therefore, you can see all the day names.

Employing All Day Names to Create an Excel VBA Calendar

Read More: How to Create a Weekly Calendar in Excel (3 Suitable Ways)


Step-7: Adding CommandButtons

In this step, we will add Command Buttons for the dates in the Excel VBA Calendar.

  • In the beginning, from the Toolbox >> we will select CommandButton.
  • Then, we will draw the CommandButton under the days.

Adding Command Button to Create an Excel VBA Calendar

  • Furthermore, we will delete the name of the CommandButton.
  • Therefore, the CommandButton looks like the following picture.

  • In addition, we draw 41 Command Buttons.
  • Therefore, the total number of CommandButton is 42.
  • These commandbuttons will indicate the dates.
Note: You must insert the command button serially. Otherwise, the calendar will not run properly.


Step-8: Inserting Codes for CommandButtons

In this step, we will insert codes for CommandButtons.

  • In the beginning, we will double-click on the empty space in the Excel VBA Calendar.

  • Then, we will type the following code.
Private Sub D_Display()

 Dim D_Initial As Date
 D_Initial = VBA.DateValue("1-" & Me.Cmb_Month.Value _
 & "-" & Me.Cmb_Year.Value)

 Dim D_Final As Integer
 D_Final = VBA.Day(VBA.DateSerial(VBA.Year(D_Initial), _
 VBA.Month(D_Initial) + 1, 1) - 1)

 Dim C_Month As Integer
 Dim C_Date As CommandButton

 For C_Month = 1 To 42
  Set C_Date = Me.Controls("CommandButton" & C_Month)
  C_Date.Caption = ""
 Next C_Month

 For C_Month = 1 To 7
  Set C_Date = Me.Controls("CommandButton" & C_Month)
   If VBA.Weekday(D_Initial) = C_Month Then
 C_Date.Caption = "1"

  Else
    C_Date.Caption = ""
 End If

 Next C_Month

  Dim C_Date1 As CommandButton
  Dim C_Date2 As CommandButton

 For C_Month = 1 To 41
 Set C_Date1 = Me.Controls("CommandButton" & C_Month)
 Set C_Date2 = Me.Controls("CommandButton" & C_Month + 1)

 If C_Date1.Caption <> "" Then
 If D_Final > C_Date1.Caption Then
 C_Date2.Caption = C_Date1.Caption + 1

    End If
 End If
  Next C_Month
End Sub

Writing Codes for Command Buttons to Create an Excel VBA Calendar

  • Moreover, we will add Call D_Display in the previous code.
  • Then, we will Run the code.

  • Therefore, you can see the Calendar is looking much more effective.
  • You can also see the dates in the Command Buttons.

  • However, the dates are fixed for the month of January and the year 2023.
  • Next, to make the calendar dynamic, we will type the two codes along with the previous code.
Private Sub Cmb_Month_Change()
If Me.Cmb_Month.Value <> "" And Me.Cmb_Year.Value <> "" Then
  Call D_Display
  Me.Month_Name.Caption = Me.Cmb_Month & "-" & Me.Cmb_Year
 End If

End Sub

Private Sub Cmb_Year_Change()
If Me.Cmb_Month.Value <> "" And Me.Cmb_Year.Value <> "" Then
    Call D_Display
    Me.Month_Name.Caption = Me.Cmb_Month & "-" & Me.Cmb_Year
End If

End Sub

Writing Codes for Combo Boxes to Create an Excel VBA Calendar

  • Next, we will again Run the code.
  • Now, we will change the month to April.

  • Therefore, you can see the calendar for April 2023.
  • Here, you can change the Year as well.


Step-9: Writing Codes for Left & Right Arrows

In this step, we will add codes to the Left and Right arrows to make the Excel VBA Calendar more dynamic and user-friendly.

  • In the beginning, we will double-click on the Right-arow.

Inserting Code for Right Arrow to Create an Excel VBA Calendar

This will open a Private Sub.

  • Then, we will type the following code.
Private Sub Next_Month_Click()
  On Error Resume Next
 If Me.Cmb_Month.ListIndex = 11 Then
    Me.Cmb_Month.ListIndex = 0
   Me.Cmb_Year.Value = Me.Cmb_Year.Value + 1
Else
   Me.Cmb_Month.ListIndex = Me.Cmb_Month.ListIndex + 1
 End If
End Sub

  • In addition, we will double-click on the Left arrow.

  • Moreover, we will type the following code.
Private Sub Previous_Month_Click()
  On Error Resume Next
  If Me.Cmb_Month.ListIndex = 0 Then
    Me.Cmb_Month.ListIndex = 11
  Me.Cmb_Year.Value = Me.Cmb_Year.Value - 1
 Else
  Me.Cmb_Month.ListIndex = Me.Cmb_Month.ListIndex - 1
  End If
End Sub

  • Now, Run the code.
  • At this point, click on the Left arrow.

  • Therefore, the previous month will be shown.

  • Along with that, click on the Right arrow.

  •  As a result, the next month will be shown.


Step-10: Adding Color to Make Calendar Attractive

In this step, we will use code to add color to the Excel VBA Calendar. This will make the calendar more eye-catching.

  • First of all, we will add the following code at the end of the previous code.
Sub D_Col()

Dim C_Month As Integer
Dim C_Date As MSForms.CommandButton
For C_Month = 1 To 42
Set C_Date = Me.Controls("CommandButton" & C_Month)
C_Date.BackColor = VBA.RGB(217, 210, 233)
C_Date.Enabled = True
 Next C_Month
End Sub

Adding Code for Color to Create an Excel VBA Calendar

  • In addition, we will have to add Call D_Col to the code that is under the D_Display sub.

  • Next, the final code for creating a calendar becomes
Private Sub Cmb_Month_Change()
If Me.Cmb_Month.Value <> "" And Me.Cmb_Year.Value <> "" Then
Call D_Display
Me.Month_Name.Caption = Me.Cmb_Month & "-" & Me.Cmb_Year
End If
End Sub
Private Sub Cmb_Year_Change()
If Me.Cmb_Month.Value <> "" And Me.Cmb_Year.Value <> "" Then
Call D_Display
Me.Month_Name.Caption = Me.Cmb_Month & "-" & Me.Cmb_Year
End If
End Sub

Private Sub Next_Month_Click()
On Error Resume Next
If Me.Cmb_Month.ListIndex = 11 Then
Me.Cmb_Month.ListIndex = 0
Me.Cmb_Year.Value = Me.Cmb_Year.Value + 1
Else
Me.Cmb_Month.ListIndex = Me.Cmb_Month.ListIndex + 1
End If
End Sub

Private Sub Previous_Month_Click()
On Error Resume Next
If Me.Cmb_Month.ListIndex = 0 Then
Me.Cmb_Month.ListIndex = 11
Me.Cmb_Year.Value = Me.Cmb_Year.Value - 1
Else
Me.Cmb_Month.ListIndex = Me.Cmb_Month.ListIndex - 1
End If
End Sub

Private Sub UserForm_Initialize()
Dim C_Month As Integer
For C_Month = 1 To 12
Me.Cmb_Month.AddItem VBA.Format(VBA.DateSerial(2020, C_Month, 1), "MMMM")
Next C_Month
Me.Cmb_Month.Value = VBA.Format(VBA.Date, "MMMM")
For C_Month = VBA.Year(Date) - 20 To VBA.Year(Date) + 20
Me.Cmb_Year.AddItem C_Month
Next C_Month
Me.Cmb_Year.Value = VBA.Format(VBA.Date, "YYYY")
Call D_Display
End Sub

Private Sub D_Display()
Dim D_Initial As Date
D_Initial = VBA.DateValue("1-" & Me.Cmb_Month.Value _
& "-" & Me.Cmb_Year.Value)
Dim D_Final As Integer
D_Final = VBA.Day(VBA.DateSerial(VBA.Year(D_Initial), _
VBA.Month(D_Initial) + 1, 1) - 1)
Dim C_Month As Integer
Dim C_Date As CommandButton
For C_Month = 1 To 42
Set C_Date = Me.Controls("CommandButton" & C_Month)
C_Date.Caption = ""
Next C_Month
For C_Month = 1 To 7
Set C_Date = Me.Controls("CommandButton" & C_Month)
If VBA.Weekday(D_Initial) = C_Month Then
C_Date.Caption = "1"
Else
C_Date.Caption = ""
End If
Next C_Month
Dim C_Date1 As CommandButton
Dim C_Date2 As CommandButton
For C_Month = 1 To 41
Set C_Date1 = Me.Controls("CommandButton" & C_Month)
Set C_Date2 = Me.Controls("CommandButton" & C_Month + 1)
If C_Date1.Caption <> "" Then
If D_Final > C_Date1.Caption Then
C_Date2.Caption = C_Date1.Caption + 1
End If
End If
Next C_Month
Call D_Col
End Sub

Sub D_Col()
Dim C_Month As Integer
Dim C_Date As MSForms.CommandButton
For C_Month = 1 To 42
Set C_Date = Me.Controls("CommandButton" & C_Month)
C_Date.BackColor = VBA.RGB(217, 210, 233)
C_Date.Enabled = True
Next C_Month
End Sub
  • At this point, Run the code.
  • Therefore, you can see the Calendar.


Practice Section

You can download the above Excel file and practice the explained steps.


Conclusion

Here, we show you 10 easy steps for Excel VBA Calendar. Thank you for reading this article. We hope it was helpful. If you have any queries, please let us know in the comment section. You can visit our website Exceldemy for more related articles.


Related Articles

Afia Kona

Afia Kona

Hello, I am Afia Aziz Kona. I graduated in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology (BUET). I have an immense interest in technical writing and content development, therefore, I am working as a content developer at Exceldemy. In my spare time, I travel, watch movies, and cook different dishes.

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo