Split worksheet into tabs then email

Shelley2018

New Member
Joined
Oct 28, 2018
Messages
3
Hi everybody, I hope someone can help with a code for this please?

In the example below, I would like to split a worksheet at each change in the Location column so that the data goes into a new sheet of the same workbook, then email each new tab to the relevant email in cell E2 using Outlook.

I’ve tried using the responses to other similar questions and sort of got it to work but not quite. I am also struggling with the email it defaults from – I changed my default email in Outlook first but it was still going from my previously set default email. Is there any way to specify which email account it comes from? Would it also be possible to add a total to the cost column once it goes into a new tab? This isn't essential so ignore this if its faffy

Thank you

NameDOBLocationCostEmail
A Jones01/02/1977London12me@myemail.com
B Jones02/02/1988London14me@myemail.com
C Davis03/05/1966Manchester144me1@myemail.com
D Taylor02/06/1933Manchester13me1@myemail.com
E Jones03/04/1933Birmingham15me2@myemail.com
F Smith04/05/1965Birmingham56me2@myemail.com
G Smith03/04/1934Birmingham4me2@myemail.com

<tbody>
</tbody>
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi & welcome to MrExcel.
How about
Code:
Sub CopyFltr()
   Dim ws As Worksheet
   Dim Cl As Range
   
   Application.ScreenUpdating = False
   Set ws = Sheets("[COLOR=#ff0000]pcode[/COLOR]")
   If ws.AutoFilterMode Then ws.AutoFilterMode = False
   With CreateObject("Scripting.dictionary")
      For Each Cl In ws.Range("C2", ws.Range("C" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            .Add Cl.Value, Nothing
            ws.Range("[COLOR=#ff0000]A1:Q1[/COLOR]").AutoFilter 3, Cl.Value
            Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
            ws.AutoFilter.Range.Copy Sheets(Cl.Value).Range("A1")
            Sheets(Cl.Value).Range("D" & Rows.Count).End(xlUp).Offset(1).FormulaR1C1 = "=sum(r2c:r[-1]c)"
         End If
      Next Cl
   End With
   ws.AutoFilterMode = False
End Sub
Change values in red to suit.
This will create the new sheets & add the total.
I can't help with the email side as I don't have Outlook, but have a look here https://www.rondebruin.nl/win/s1/outlook/mail.htm
 
Upvote 0
Thank you very much for that, it works well. Please could you tell me if there is a way to make it autofit the column widths in the new tabs?
 
Upvote 0
Add the line in blue
Code:
Sub CopyFltr()
   Dim ws As Worksheet
   Dim Cl As Range
   
   Application.ScreenUpdating = False
   Set ws = Sheets("pcode")
   If ws.AutoFilterMode Then ws.AutoFilterMode = False
   With CreateObject("Scripting.dictionary")
      For Each Cl In ws.Range("C2", ws.Range("C" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            .Add Cl.Value, Nothing
            ws.Range("A1:Q1").AutoFilter 3, Cl.Value
            Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
            ws.AutoFilter.Range.Copy Sheets(Cl.Value).Range("A1")
            Sheets(Cl.Value).Range("D" & Rows.Count).End(xlUp).Offset(1).FormulaR1C1 = "=sum(r2c:r[-1]c)"
            [COLOR=#0000ff]Sheets(Cl.Value).Columns.AutoFit[/COLOR]
         End If
      Next Cl
   End With
   ws.AutoFilterMode = False
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

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