Emailing report for each item in filter/slicer

psl1089

New Member
Joined
Apr 24, 2018
Messages
6
Hi all,

Hopefully I can explain this well enough - I have a dashboard based off of tables on other sheets which can be filtered to show results for individual departments. I also have corresponding email addresses for each department that the filtered results need to be send to.

Is there any way I can loop through each filter item and automatically send to the proper recipients? I can partially brute force my way through it with the macro recorder to at least make a copy of the workbook for each department, but that would still mean manually emailing out 50+ files.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi and welcome to the Board


  • Do you have a slicer with the department names? Is the procedure to click the item and then email the filtered results?
  • Do you have a worksheet table with departments and corresponding emails?
  • What do you need to send: a range, a worksheet or the entire workbook?
 
Upvote 0
This example creates one email message for each slicer item:

Code:
Sub MailReports()
Dim i%, j%, sc As SlicerCache, olapp As Object, olmail As Object, sdata As Worksheet, res
Set olapp = CreateObject("Outlook.Application")
Set sdata = Sheets("table")                                 ' where mail addresses are
Set sc = ActiveWorkbook.SlicerCaches("Dept_Slicer")         ' your slicer name here
For i = 1 To sc.SlicerItems.Count
    sc.ClearManualFilter
    sc.SlicerItems(i).Selected = True
    For j = 1 To sc.SlicerItems.Count
        If j <> i Then sc.SlicerItems(j).Selected = False
    Next
    ActiveWorkbook.Save
    Set olmail = olapp.CreateItem(0)
    res = Application.VLookup(sc.SlicerItems(i).Name, sdata.[H1:I99], 2, False)
    If IsError(res) Then
        MsgBox "Could not find email address"
        Exit Sub
    End If
    With olmail
        .To = res                                           ' address previously found
        .Subject = "Report"
        .body = "Department " & sc.SlicerItems(i).Name
        .Attachments.Add Application.ActiveWorkbook.FullName
        .display                                            ' use Send here
    End With
Next
Set olmail = Nothing
Set olapp = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top