Emailing reports based on multiple drop down box conditions

soconfused365

New Member
Joined
May 16, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am incredibly confused here!

I have a workbook with a sheet for each month of the year Jan,Feb,Mar etc and in each one is multiple names in column A over and over (lots of other info in the other columns but its all related to the column A name). I have a reporting tab and from that I need to have 2 buttons - one send to all and 1 send to specific

Specific - I need to unprotect the sheet for the month that is selected in the drop down box, filter it to the persons name in the 2nd drop down box, copy that filtered worksheet to another workbook on its own, lock it again so they cant see the other information in there and then email it to them

Send to all - Same as above but it does 1 after the other of all the names within the list

I have a lookups sheet with each of their names next to their email address. I do not need to save the resulting sheets. All sheets have the same password to unprotect. All possible names are within the list and there is no variance on them. Same with the months drop down - they are exactly the same as the tabs.

Message within the email body also needs to refer to the month drop down eg Hi *NAME* Please see attached *Jan*

I have found lots of little bits that can do 1 of these jobs but then when I try and put them all together it goes all higgledy-piggledy so any help would be greatly appreciated! On the latest updating 365 version and if you need to know anything else just ask!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
What you're asking for is very possible, however, I wouldn't be prepared to even start on this until I saw what your "reporting tab" looks like, and a sample of one of your month sheets. This forum has the XL2BB Tool which enables you to provide samples of your sheets. You can disguise any personal/sensitive information if needed.
 
Upvote 0
What you're asking for is very possible, however, I wouldn't be prepared to even start on this until I saw what your "reporting tab" looks like, and a sample of one of your month sheets. This forum has the XL2BB Tool which enables you to provide samples of your sheets. You can disguise any personal/sensitive information if needed.
tester.xlsx
ABCDEFGHIJKLMNOPQRSTU
1Name DateTime 1Time 2 Time 3Time 4 Thing 1 Thing 2Thing 3Thing 4Thing 5 Thing 6Total Things Thing 1 Thing 2Thing 3Thing 4Thing 5Thing 6Total thingsFinal calc
2Thunderbirds01/01/2022621201214227168044800220183.33%
3Stingray784204334634421000036586.90%
4Scarlet424089170088081016970.42%
5Thunderbirds03/01/20226236085522320350200227721.39%
6Stingray31806897845440992800671372.78%
7Scarlet4824079160490081013054.17%
8Thunderbirds04/01/202274205813400032007217.14%
9Stingray9540382455564022004513124.26%
10Scarlet3418038311056012006837.78%
11Thunderbirds05/01/202284802791801477081017235.83%
12Stingray848079514720020009219.17%
13Scarlet212032932240000295344.17%
14Thunderbirds06/01/202272420714120491103609622.86%
15Stingray31804711320028006033.33%
16Scarlet84802232501400207022146.04%
17Thunderbirds07/01/202210600225914005536009115.17%
18Stingray212085720643577000176146.67%
19Scarlet42407121904900108015765.42%
20Thunderbirds10/01/20228480892130063000218417.50%
21Stingray318089170088081016993.89%
22Scarlet742031114240044006816.19%
Jan
Cell Formulas
RangeFormula
M2:M22,T2:T22M2=SUM(G2:L2)
N2:N22N2=G2*Lookups!$B$1
O2:O22O2=H2*Lookups!$B$2
P2:P22P2=I2*Lookups!$B$3
Q2:Q22Q2=J2*Lookups!$B$4
R2:R22R2=K2*Lookups!$B$5
S2:S22S2=L2*Lookups!$B$6
U2:U22U2=T2/E2
B5,B20,B17,B14,B11,B8B5=WORKDAY(B2,1)
E2E2=C2*20
E3:E22E3=C3*60


tester.xlsx
ABCDEFG
1Thing 1 8Jan
2Thing 2 7Feb
3Thing 3 11Mar
4Thing 44Apr
5Thing 5 9May
6Thing 61Jun
7Jul
8Aug
9Sept
10Thunderbirdsthirdbirds@arego.comOct
11Stingraystingray@stingray.co.ukNov
12Scarletscarlet@captain.comDec
13
14
15
Lookups


tester.xlsx
ABCDEFGHI
1Report 1
2
3Button to activate an exisiting Macro
4
5
6
7Report 2
8
9Button to activate an exisiting Macro
10
11
12
13
14Reports I need help with
15Button here to run the to all
16Month
17
18Name
19
20
21Button here to run the single
22
23
24
Reporting
Cells with Data Validation
CellAllowCriteria
C16List=Lookups!$F$1:$F$12
C18List=Lookups!$A$10:$A$12




Anything in Yellow is typed in or selected from a drop down box. Oh I did also remember that at the end the sheets will need reprotecting with the same password - thank you so so much! If there is anything more you need let me know :)
 
Upvote 0
OK, this might take a bit of back & forth. This first code is to send an individual sheet to one person. To test it, make sure you have a valid email address next to the person you select. Once we get this right, we'll go onto the multiple emails version.
EDIT - forgot the protect/unprotect part.

VBA Code:
Option Explicit
Sub soconfused365_1()
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet
    Dim NameRng As Range, c As Range, wb As Workbook, lr As Long
    Dim RepMonth As String, EmailTo As String, EmailAddress As String

    Set ws1 = Worksheets("Reporting")
    Set ws2 = Worksheets("Lookups")
    Set NameRng = ws2.Range("A10", ws2.Cells(Rows.Count, "A").End(xlUp))

    'Get names and month variables
    RepMonth = ws1.Range("C16")
    EmailTo = ws1.Range("C18")

    'Ensure no blank variables
    If Len(RepMonth) = 0 Or Len(EmailTo) = 0 Then
        MsgBox "Select a valid month and name"
        Exit Sub
    End If

    'Get the email address and the report sheet name
    EmailAddress = NameRng.Cells(Application.Match(EmailTo, NameRng, 0), 1).Offset(, 1)
    Set ws3 = Worksheets(RepMonth)
    ws3.Unprotect "password"
    
    'Copy the month sheet to a new workbook
    ws3.Copy: Set ws4 = ActiveWorkbook.Sheets(1)
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & RepMonth & ".xlsx"
    ws3.Protect "password"
    
    'Unmerge cells & fill in dates
    ws4.Cells.UnMerge
    lr = ws4.Cells(Rows.Count, 1).End(3).Row
    For Each c In ws4.Range("B2:B" & lr)
        If c = "" Then c = c.Offset(-1)
    Next c

    'Set the filter on the month sheet - delete all other records
    With ws4.Cells(1).CurrentRegion
        .AutoFilter 1, "<>" & EmailTo
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
    ActiveWorkbook.Save

    'Email the individual report
    Dim OutApp As Object, OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = EmailAddress
        .Subject = RepMonth & " Report attached"
        .Body = "Hi " & EmailTo & vbNewLine & "Please see attached " & RepMonth
        .Attachments.Add ActiveWorkbook.FullName
        .Send
    End With

    'Delete the individual report
    ActiveWorkbook.Close 0
    Kill ThisWorkbook.Path & "\" & RepMonth & ".xlsx"

End Sub
 
Last edited:
Upvote 0
Wow that worked 99% immediately!!!!

Seems like it needs a copy and paste in there somewhere as it is throwing N/As on the recipient's screen in some of the formulas

The filtering and email all worked perfectly!
 
Upvote 0
Wow that worked 99% immediately!!!!

Seems like it needs a copy and paste in there somewhere as it is throwing N/As on the recipient's screen in some of the formulas

The filtering and email all worked perfectly!

Thanks for the feedback. Slight addition to the code, try this:

VBA Code:
Option Explicit
Sub soconfused365_2()
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet
    Dim NameRng As Range, c As Range, wb As Workbook, lr As Long
    Dim RepMonth As String, EmailTo As String, EmailAddress As String

    Set ws1 = Worksheets("Reporting")
    Set ws2 = Worksheets("Lookups")
    Set NameRng = ws2.Range("A10", ws2.Cells(Rows.Count, "A").End(xlUp))

    'Get names and month variables
    RepMonth = ws1.Range("C16")
    EmailTo = ws1.Range("C18")

    'Ensure no blank variables
    If Len(RepMonth) = 0 Or Len(EmailTo) = 0 Then
        MsgBox "Select a valid month and name"
        Exit Sub
    End If

    'Get the email address and the report sheet name
    EmailAddress = NameRng.Cells(Application.Match(EmailTo, NameRng, 0), 1).Offset(, 1)
    Set ws3 = Worksheets(RepMonth)
    ws3.Unprotect "password"
    
    'Copy the month sheet to a new workbook
    ws3.Copy: Set ws4 = ActiveWorkbook.Sheets(1)
    With ws4.UsedRange
        .Value = .Value
    End With
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & RepMonth & ".xlsx"
    ws3.Protect "password"
    
    'Unmerge cells & fill in dates
    ws4.Cells.UnMerge
    lr = ws4.Cells(Rows.Count, 1).End(3).Row
    For Each c In ws4.Range("B2:B" & lr)
        If c = "" Then c = c.Offset(-1)
    Next c

    'Set the filter on the month sheet - delete all other records
    With ws4.Cells(1).CurrentRegion
        .AutoFilter 1, "<>" & EmailTo
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
    ActiveWorkbook.Save

    'Email the individual report
    Dim OutApp As Object, OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = EmailAddress
        .Subject = RepMonth & " Report attached"
        .Body = "Hi " & EmailTo & vbNewLine & "Please see attached " & RepMonth
        .Attachments.Add ActiveWorkbook.FullName
        .Send
    End With

    'Delete the individual report
    ActiveWorkbook.Close 0
    Kill ThisWorkbook.Path & "\" & RepMonth & ".xlsx"

End Sub
 
Upvote 0
When you're ready, you can try the following code to send the same sheet to all the recipients on your list:

VBA Code:
Option Explicit
Sub soconfused365_multi()
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet
    Dim NameRng As Range, c As Range, r As Range, wb As Workbook, lr As Long
    Dim RepMonth As String, EmailTo As String, EmailAddress As String
    Application.ScreenUpdating = False
    
    Set ws1 = Worksheets("Reporting")
    Set ws2 = Worksheets("Lookups")
    Set NameRng = ws2.Range("A10", ws2.Cells(Rows.Count, "A").End(xlUp))

    'Get month variables
    RepMonth = ws1.Range("C16")
    
    'Loop through names variables
    For Each r In NameRng
    
        EmailTo = r.Value
    
        'Ensure no blank variables
        If Len(RepMonth) = 0 Or Len(EmailTo) = 0 Then
            MsgBox "Valid month and/or name missing"
            Exit Sub
        End If
    
        'Get the email address and the report sheet name
        EmailAddress = NameRng.Cells(Application.Match(EmailTo, NameRng, 0), 1).Offset(, 1)
        Set ws3 = Worksheets(RepMonth)
        ws3.Unprotect "password"
        
        'Copy the month sheet to a new workbook
        ws3.Copy: Set ws4 = ActiveWorkbook.Sheets(1)
        With ws4.UsedRange
            .Value = .Value
        End With
        ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & RepMonth & ".xlsx"
        ws3.Protect "password"
        
        'Unmerge cells & fill in dates
        ws4.Cells.UnMerge
        lr = ws4.Cells(Rows.Count, 1).End(3).Row
        For Each c In ws4.Range("B2:B" & lr)
            If c = "" Then c = c.Offset(-1)
        Next c
    
        'Set the filter on the month sheet - delete all other records
        With ws4.Cells(1).CurrentRegion
            .AutoFilter 1, "<>" & EmailTo
            .Offset(1).EntireRow.Delete
            .AutoFilter
        End With
        ActiveWorkbook.Save
    
        'Email the individual report
        Dim OutApp As Object, OutMail As Object
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .To = EmailAddress
            .Subject = RepMonth & " Report attached"
            .Body = "Hi " & EmailTo & vbNewLine & "Please see attached " & RepMonth
            .Attachments.Add ActiveWorkbook.FullName
            .Send
        End With
    
        'Delete the individual report
        ActiveWorkbook.Close 0
        Kill ThisWorkbook.Path & "\" & RepMonth & ".xlsx"

    Next r
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I will give these a shot in the morning and let you know how they go! Thanks again for your help
 
Upvote 0
The single is working perfectly now thank you!

With the multiple I am getting a run time error
Outlook does not recognise 1 or more of the names. This is fed from the same list as the single ones. When I click on debug it takes me to

.Send

Right at the end under the email the individual report section

Any ideas?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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