> For the complete documentation index, see [llms.txt](https://code.cyberchamp.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://code.cyberchamp.net/outlook/vba-create-a-group-of-year-and-month-in-outlook.md).

# VBA: Create a group of Year and Month in Outlook

* Create a **New Colum** which name "**GroupY**" and "**GroupYM**"
* Open the Microsoft Visual Basic for Applications from "Developer" tab
* Insert > Module&#x20;
* &#x20;Use VBA macro into the module:

```
Sub ListSelectionYearAndMonth()
    Dim aObj As Object
    Dim oProp As Outlook.UserProperty
    Dim sMonth
    Dim sYear
    
    On Error Resume Next
    
    For Each aObj In Application.ActiveExplorer.Selection
        Set oMail = aObj
        
        sMonth = Month(oMail.ReceivedTime)
        sYear = Year(oMail.ReceivedTime)
        Set oProp = oMail.UserProperties.Add("GroupY", olText, True)
        oProp.Value = sYear
        Set oProp = oMail.UserProperties.Add("GroupYM", olText, True)
        oProp.Value = Join(Array(sYear, "-", sMonth))
        
        oMail.Save
        
        Err.Clear
    Next
    
End Sub

```
