Send multiple excel worksheets using a control sheet

JSF_99

New Member
Joined
May 23, 2022
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
Hi,

I would like to send multiple worksheets based on a control sheet.

I have put together an example (attached)..... ideally would like the vba to loop through the various Rows till it reaches the end of the table.

Thanks

JSF
 

Attachments

  • Example Test.PNG
    Example Test.PNG
    26.3 KB · Views: 22

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this macro:
VBA Code:
Public Sub Email_Worksheets()

    Dim OutApp As Object, OutEmail As Object
    Dim r As Long
    Dim tempWorkbookFullName As String
    Dim emailSubject As String, toEmailAddresses As String

    Set OutApp = CreateObject("Outlook.Application")
    
    With Worksheets("Control Sheet")
    
        For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
        
            emailSubject = .Cells(r, "A").Value
            toEmailAddresses = .Cells(r, "B").Value
            
            tempWorkbookFullName = Environ("temp") & "\" & .Cells(r, "A").Value & ".xlsx"
            If Dir(tempWorkbookFullName) <> vbNullString Then Kill tempWorkbookFullName

            Worksheets(Split(.Cells(r, "C").Value, ";")).Copy
            ActiveWorkbook.SaveAs Filename:=tempWorkbookFullName
            ActiveWorkbook.Close False
            
            Set OutEmail = OutApp.createItem(0)
            
            With OutEmail
                .To = toEmailAddresses
                .Subject = emailSubject
                .Body = "This is the email body text."
                .Attachments.Add tempWorkbookFullName
                .Display  'or .Send
            End With
            
        Next
        
    End With
   
    Kill tempWorkbookFullName
    
    MsgBox "Done"
    
End Sub
 
Upvote 0
Try this macro:
VBA Code:
Public Sub Email_Worksheets()

    Dim OutApp As Object, OutEmail As Object
    Dim r As Long
    Dim tempWorkbookFullName As String
    Dim emailSubject As String, toEmailAddresses As String

    Set OutApp = CreateObject("Outlook.Application")
   
    With Worksheets("Control Sheet")
   
        For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
       
            emailSubject = .Cells(r, "A").Value
            toEmailAddresses = .Cells(r, "B").Value
           
            tempWorkbookFullName = Environ("temp") & "\" & .Cells(r, "A").Value & ".xlsx"
            If Dir(tempWorkbookFullName) <> vbNullString Then Kill tempWorkbookFullName

            Worksheets(Split(.Cells(r, "C").Value, ";")).Copy
            ActiveWorkbook.SaveAs Filename:=tempWorkbookFullName
            ActiveWorkbook.Close False
           
            Set OutEmail = OutApp.createItem(0)
           
            With OutEmail
                .To = toEmailAddresses
                .Subject = emailSubject
                .Body = "This is the email body text."
                .Attachments.Add tempWorkbookFullName
                .Display  'or .Send
            End With
           
        Next
       
    End With
  
    Kill tempWorkbookFullName
   
    MsgBox "Done"
   
End Sub
Hi John,
Thank you very much for your reply.
Works exactly as i had imagined it.
Thanks
JSF
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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