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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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