Split workbook into individual worksheets and email via outlook to different recipients.

CFlack8472

New Member
Joined
Jul 23, 2012
Messages
6
Hi, I am trying to split a workbook with multiple worksheets to separate worksheets with one macro and then email (via outlook) the individual worksheets to different people.

The objective of this is for recipients to receive information on one sheet only. They are not allowed to see the rest of the workbook (a Chinese wall) - I am not allowed to email the entire workbook. We are currently manually moving five worksheets to their own individual workbook and then copying and pasting each of these workbooks as an attachment and sending out 5 emails to different individuals. This process is prone to human error as we are occasionally emailing the wrong worksheet or accidentally emailing the whole workbook or sending a workbook to the wrong recipient etc, a macro accomplishing this would eliminate human error entirely and save about 10-15 minutes a day.

I have tried to utilise similar macros but none of them seem to split multiple different worksheets to different recipients. The macro below is the closest I have found. It splits out and emails one sheet. I need it to loop through the individual worksheets in the workbook and then send each one to a different recipient.

Each recipient will only receive one worksheet from a larger workbook. Hopefully this is a reasonably simple piece of VBA.

Code:
Sub Mail_ActiveSheet()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object


    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With


    Set Sourcewb = ActiveWorkbook


    'Copy the ActiveSheet to a new workbook
    ActiveSheet.Copy
    Set Destwb = ActiveWorkbook


    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2016
            Select Case Sourcewb.FileFormat
            Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
            Case 52:
                If .HasVBProject Then
                    FileExtStr = ".xlsm": FileFormatNum = 52
                Else
                    FileExtStr = ".xlsx": FileFormatNum = 51
                End If
            Case 56: FileExtStr = ".xls": FileFormatNum = 56
            Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
            End Select
        End If
    End With


    '    'Change all cells in the worksheet to values if you want
    '    With Destwb.Sheets(1).UsedRange
    '        .Cells.Copy
    '        .Cells.PasteSpecial xlPasteValues
    '        .Cells(1).Select
    '    End With
    '    Application.CutCopyMode = False


    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .to = "ron@debruin.nl"
            .CC = ""
            .BCC = ""
            .Subject = "This is the Subject line"
            .Body = "Hi there"
            .Attachments.Add Destwb.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Send   'or use .Display
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With


    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr


    Set OutMail = Nothing
    Set OutApp = Nothing


    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I need several data:
- Do you have other macros running?
- How to know which sheet goes to which email, that is, where do I get the email?
- All sheets of the file will be sent or only some?
- What will each file be called?
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...look-to-different-recipients.html#post5173212

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...look-to-different-recipients.html#post5173212

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.

Thank you, apologies. I was also informed cross posting should be mentioned on excelforum.com
 
Upvote 0
Hello, thanks for getting back to me. Please forgive my lack of knowledge of VBA.

I need several data:
- Do you have other macros running?

No other macros running. The one I quoted above does virtually needed apart from looping through the sheets and duplicating its functionality. If an improved version of the above macro could run through multiple worksheets and send an email of each worksheet in the workbook it would make my life a lot more pleasant.

- How to know which sheet goes to which email, that is, where do I get the email?

The sheets will be titled as follows. It is sent to a distribution list so the below will work if typed into the ‘to’ section in outlook
MWB Part 3c_BIDS NO PRICING to Bid_pricing_group
MWB Part 4a_Bid eval input to shipping_mail
MWB Part 3c_BIDS PRICING to planning_folio_mail
MWB Part 3a_Bidder codes to Credit_mail

Is it possible to use a variation of the below and state the emails within the VBA module? This would avoid the need to create a separate worksheet where the emails are stated. But if I need another sheet like in the suggestion in https://www.excelforum.com/excel-pr...look-to-different-recipients.html#post5173392 then that is ok

With OutMail
.to = "ron@debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Attachments.Add Destwb.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display)

- All sheets of the file will be sent or only some?

Only some of the worksheets in the workbook.


- What will each file be called?

Again can this not be configured in the VBA code? Happy to call it file one and file 2 etc. I would know how to do that once I have a working module. I have about 5 sheets to send out to separate recipients.

Otherwise the current titles of the worksheets for each email are
MWB Part 3c_BIDS NO PRICING
MWB Part 4a_Bid eval input
MWB Part 3c_BIDS PRICING
MWB Part 3a_Bidder codes

Thanks again!
 
Last edited:
Upvote 0
Hello, thanks for getting back to me. Please forgive my lack of knowledge of VBA.



No other macros running. The one I quoted above does virtually needed apart from looping through the sheets and duplicating its functionality. If an improved version of the above macro could run through multiple worksheets and send an email of each worksheet in the workbook it would make my life a lot more pleasant.



The sheets will be titled as follows. It is sent to a distribution list so the below will work if typed into the ‘to’ section in outlook
MWB Part 3c_BIDS NO PRICING to Bid_pricing_group
MWB Part 4a_Bid eval input to shipping_mail
MWB Part 3c_BIDS PRICING to planning_folio_mail
MWB Part 3a_Bidder codes to Credit_mail

Is it possible to use a variation of the below and state the emails within the VBA module? This would avoid the need to create a separate worksheet where the emails are stated. But if I need another sheet like in the suggestion in https://www.excelforum.com/excel-pr...look-to-different-recipients.html#post5173392 then that is ok

With OutMail
.to = "ron@debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Attachments.Add Destwb.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display)



Only some of the worksheets in the workbook.




Again can this not be configured in the VBA code? Happy to call it file one and file 2 etc. I would know how to do that once I have a working module. I have about 5 sheets to send out to separate recipients.

Otherwise the current titles of the worksheets for each email are
MWB Part 3c_BIDS NO PRICING
MWB Part 4a_Bid eval input
MWB Part 3c_BIDS PRICING
MWB Part 3a_Bidder codes

Thanks again!

Actually I think the macro provided by sintek in https://www.excelforum.com/excel-pr...look-to-different-recipients.html#post5173392 works. Let me try it and get back to you! Please don't work on this any further until I have evaluated it.
 
Last edited:
Upvote 0
The solution is: please let me know how I set this to solved. Thank you for your help.

Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Option Explicit

Sub EmailSheet()
Dim i As Long, Path As String, Employee As String, Email As String, File As String, Body As String
Path = ThisWorkbook.Path
Application.ScreenUpdating = False
With Sheets("MasterList")
    For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
        Employee = .Range("B" & i): Email = .Range("C" & i): File = .Range("A" & i).Text: Body = .Range("D" & i).Text
        Sheets("" & .Range("A" & i).Value & "").Copy
        ActiveWorkbook.SaveAs Path & "" & File & ".xlsx"
        With CreateObject("outlook.application").CreateItem(0)
            .To = Email
            .Subject = "Whatever you like"
             .HTMLBody = Body & .HTMLBody
            .Attachments.Add Path & "" & File & ".xlsx"
            .Display 'Change to Send after testing
        End With
        ActiveWorkbook.Close saveChanges:=False
        Kill Path & "" & File & ".xlsx"
    Next i
End With
Application.ScreenUpdating = True
End Sub
</code>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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