Vba loop through each sheet in workbook, paste all into email address in active sheet

DSEIDEN76

New Member
Joined
Oct 8, 2018
Messages
11
VBA rookie back for help. Not sure if what I'm trying to do is possible (or at least as simple as I'm trying to make it).
I have a workbook where I have a macro that splits a main sheet into new sheets based on unique email addresses. I now want to email each sheet based on the email address in cell C2 of each sheet. Would prefer to copy all visible cells in the sheet and paste into .HTMLBody but if I have to save and attach that is okay too. Currently my code is opening Outlook and using the email address in cell C2 of the last sheet, rather than looping through each sheet, and it isn't pasting anything. Thank you in advance for your help.

Code:
Sub
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim Work As Worksheet
Dim Lastrow As String


Lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row






Set rng = Nothing
On Error Resume Next
Set rng = ActiveSheet.Range("A1:D" & Lastrow).Select
On Error GoTo 0


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


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


On Error Resume Next


With OutMail
For Each Work In Worksheets
    .To = ActiveSheet.Range("C2")
    .CC = "test@ferguson.com"
    .Subject = "Test RGA Request"
    .HTMLBody = rng & ActiveSheet.PasteSpecial
    .Display
Next Work
End With
On Error GoTo 0


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




   
   
End Sub
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

DSEIDEN76

New Member
Joined
Oct 8, 2018
Messages
11
Kenneth, thank you for sharing Ron's link. I used that to create much of my code, but I don't understand how to call the function into the sub. He has a function and a sub that don't seem to work in tandem.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,424
Messages
5,528,685
Members
409,830
Latest member
KT50

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top