Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88809

Count all Outlook emails from each sender then display by year and month in Excel

$
0
0

Is there a way I could (in Excel) count all Outlook emails from each sender then display by year and month in Excel?

I have been trying to edit the code below, but I am not sure if I'm on the right track.

At the moment I'm using Excel 2010 and Outlook 2010, if that helps.

Sub EmailCount()
Dim objOutlook As Object
Dim objnSpace As Object
Dim objFolder As MAPIFolder
Dim dateStr As String
Dim myItems As Outlook.Items
Dim dict As Object
Dim msg As String
Dim NextRow As Long
Dim FirstRow As Long
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")

On Error Resume Next
'Set objFolder = objnSpace.Folders("Personal Folders").Folders("Inbox")
Set objFolder = objnSpace.Folders("Inbox")
If Err.Number <> 0 Then
    Err.Clear
    MsgBox "No such folder."
    Exit Sub
End If

Set dict = CreateObject("Scripting.Dictionary")
Set myItems = objFolder.Items
myItems.Sort "[SentOn]", True
myItems.SetColumns "[SentOn]"

FirstRow = 2
ActiveSheet.Rows(FirstRow & ":"& ActiveSheet.Rows.Count).Clear
ActiveSheet.UsedRange.Borders.LineStyle = xlNone

For Each myItem In myItems
    dateStr = GetDate(myItem.SentOn)
    If Not dict.Exists(dateStr) Then
        dict(dateStr) = 0
    End If
    dict(dateStr) = CLng(dict(dateStr)) + 1
Next myItem

' Output dates that have emails
For Each o In dict.Keys
    NextRow = ActiveSheet.Range("A"& Rows.Count).End(xlUp).Row + 1
    msg = o
    ActiveSheet.Range("A"& NextRow) = msg
Next
' Output email count per day:
For Each o In dict.Keys
    NextRow = ActiveSheet.Range("B"& Rows.Count).End(xlUp).Row + 1
    msg = dict(o)
    ActiveSheet.Range("B"& NextRow) = msg
Next
With ActiveSheet.Range("A1").CurrentRegion
    .HorizontalAlignment = xlCenter
    .Borders.LineStyle = xlContinuous
    .Borders.Weight = xlThin
    .Borders.ColorIndex = xlAutomatic
End With

ActiveSheet.Columns.AutoFit
End Sub


Function GetDate(dt As Date) As String
GetDate = CDate(Day(dt) & "-"& Month(dt) & "-"& Year(dt))
End Function

Viewing all articles
Browse latest Browse all 88809

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>