Sending emails from excel based on row - Urgent

exceluser9

Active Member
Joined
Jun 27, 2015
Messages
388
Hi Team,

I have a data as below and the data starts from row 3. Row 1 and 2 are headers.

What i want macro to do is to create a new workbook for respective user and draft an email with standard text from outlook. In below example for James there should be 2 emails generated because the name is same and the email address is different.

Also if the generic mail id is different then it has to create a new workbook for this generic id.

So the logic will be based on Generic mailid, User and User email and these headers are in row 2

Generic mailid - column E
User - column - H
User email address column P

Data will be until row AZ


Text is - Please go through attached file.


Workbook should be created from row 1 till the end of the data.

[TABLE="width: 1870"]
<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD]ID[/TD]
[TD]From[/TD]
[TD]Date[/TD]
[TD]Country[/TD]
[TD]Generic mailid[/TD]
[TD]Contracter name[/TD]
[TD]Activity[/TD]
[TD]User[/TD]
[TD]Reporter[/TD]
[TD]Form[/TD]
[TD]TAT[/TD]
[TD]Days[/TD]
[TD]Form[/TD]
[TD]TAT[/TD]
[TD]Days[/TD]
[TD]User email[/TD]
[/TR]
[TR]
[TD]8675[/TD]
[TD]John[/TD]
[TD]05/03/2019[/TD]
[TD]ENG[/TD]
[TD]Roy@gmail.com[/TD]
[TD]Muller[/TD]
[TD]Setup[/TD]
[TD]Wright[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Wight@gmail.com[/TD]
[/TR]
[TR]
[TD]78678[/TD]
[TD]Kevin[/TD]
[TD]13/03/2019[/TD]
[TD]AST[/TD]
[TD]Roy@gmail.com[/TD]
[TD]Danone[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]James@gmail.com[/TD]
[/TR]
[TR]
[TD]8679[/TD]
[TD]Johny[/TD]
[TD]05/04/2019[/TD]
[TD]RS[/TD]
[TD]Roy@gmail.com[/TD]
[TD]Nestle[/TD]
[TD]Setup[/TD]
[TD]Vince[/TD]
[TD]Roy[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Vince@gmail.com[/TD]
[/TR]
[TR]
[TD]78673[/TD]
[TD]Kevin Peter[/TD]
[TD]13/02/2019[/TD]
[TD]IT[/TD]
[TD]Roy@gmail.com[/TD]
[TD]Choco[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]James@gmail.com[/TD]
[/TR]
[TR]
[TD]8787[/TD]
[TD]Paul[/TD]
[TD]13/03/2019[/TD]
[TD]IT[/TD]
[TD]Roy@gmail.com[/TD]
[TD]Dominos[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Jaison[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Jamesorg@gmail.com[/TD]
[/TR]
[TR]
[TD]86752[/TD]
[TD]John[/TD]
[TD]05/03/2019[/TD]
[TD]ENG[/TD]
[TD]Roy@ymail.com[/TD]
[TD]Muller[/TD]
[TD]Setup[/TD]
[TD]Wright[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Wight@gmail.com[/TD]
[/TR]
[TR]
[TD]786784[/TD]
[TD]Kevin[/TD]
[TD]13/03/2019[/TD]
[TD]AST[/TD]
[TD]Roy@ymail.com[/TD]
[TD]Danone[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]James@gmail.com[/TD]
[/TR]
[TR]
[TD]86796[/TD]
[TD]Johny[/TD]
[TD]05/04/2019[/TD]
[TD]RS[/TD]
[TD]Roy@ymail.com[/TD]
[TD]Nestle[/TD]
[TD]Setup[/TD]
[TD]Vince[/TD]
[TD]Roy[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Vince@gmail.com[/TD]
[/TR]
[TR]
[TD]786739[/TD]
[TD]Kevin Peter[/TD]
[TD]13/02/2019[/TD]
[TD]IT[/TD]
[TD]Roy@ymail.com[/TD]
[TD]Choco[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]James@gmail.com[/TD]
[/TR]
[TR]
[TD]87875[/TD]
[TD]Paul[/TD]
[TD]13/03/2019[/TD]
[TD]IT[/TD]
[TD]Roy@ymail.com[/TD]
[TD]Dominos[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Jaison[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Jamesorg@gmail.com[/TD]
[/TR]
</tbody>[/TABLE]

Thanks
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I did not understand what data should go in each book.
Or is it the same book for all emails?
 
Upvote 0
Hi Dante,

After each filter based on user and email addess the visible cell data should go into the new workbook
 
Upvote 0
Then you can explain again in detail and examples: send to: subject: body: name of each book. with headers? Maybe it's obvious to you. But I have no idea about your information.
 
Upvote 0
Hi,

I require below data in attachment.

Workbook 1

[TABLE="width: 898"]
<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD]ID[/TD]
[TD]From[/TD]
[TD]Date[/TD]
[TD]Country[/TD]
[TD]Generic mailid[/TD]
[TD]Contracter name[/TD]
[TD]Activity[/TD]
[TD]User[/TD]
[TD]Reporter[/TD]
[TD]Form[/TD]
[TD]TAT[/TD]
[TD]Days[/TD]
[TD]Form[/TD]
[TD]TAT[/TD]
[TD]Days[/TD]
[TD]User email[/TD]
[/TR]
[TR]
[TD="align: right"]8787[/TD]
[TD]Paul[/TD]
[TD="align: right"]13/03/2019[/TD]
[TD]IT[/TD]
[TD]Roy@gmail.com[/TD]
[TD]Dominos[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Jaison[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Jamesorg@gmail.com[/TD]
[/TR]
[TR]
[TD="align: right"]87875[/TD]
[TD]Paul[/TD]
[TD="align: right"]13/03/2019[/TD]
[TD]IT[/TD]
[TD]Roy@ymail.com[/TD]
[TD]Dominos[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Jaison[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Jamesorg@gmail.com[/TD]
[/TR]
[TR]
[TD="align: right"][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Workbook 2

[TABLE="width: 898"]
<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD]ID[/TD]
[TD]From[/TD]
[TD]Date[/TD]
[TD]Country[/TD]
[TD]Generic mailid[/TD]
[TD]Contracter name[/TD]
[TD]Activity[/TD]
[TD]User[/TD]
[TD]Reporter[/TD]
[TD]Form[/TD]
[TD]TAT[/TD]
[TD]Days[/TD]
[TD]Form[/TD]
[TD]TAT[/TD]
[TD]Days[/TD]
[TD]User email[/TD]
[/TR]
[TR]
[TD="align: right"]78678[/TD]
[TD]Kevin[/TD]
[TD="align: right"]13/03/2019[/TD]
[TD]AST[/TD]
[TD]Roy@gmail.com[/TD]
[TD]Danone[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]James@gmail.com[/TD]
[/TR]
[TR]
[TD="align: right"]78673[/TD]
[TD]Kevin Peter[/TD]
[TD="align: right"]13/02/2019[/TD]
[TD]IT[/TD]
[TD]Roy@gmail.com[/TD]
[TD]Choco[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]James@gmail.com[/TD]
[/TR]
[TR]
[TD="align: right"]786784[/TD]
[TD]Kevin[/TD]
[TD="align: right"]13/03/2019[/TD]
[TD]AST[/TD]
[TD]Roy@ymail.com[/TD]
[TD]Danone[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]James@gmail.com[/TD]
[/TR]
[TR]
[TD="align: right"]786739[/TD]
[TD]Kevin Peter[/TD]
[TD="align: right"]13/02/2019[/TD]
[TD]IT[/TD]
[TD]Roy@ymail.com[/TD]
[TD]Choco[/TD]
[TD]Setup[/TD]
[TD]James[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]James@gmail.com[/TD]
[/TR]
</tbody>[/TABLE]

Workbook 3

[TABLE="width: 898"]
<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD="align: right"]8679[/TD]
[TD]Johny[/TD]
[TD="align: right"]05/04/2019[/TD]
[TD]RS[/TD]
[TD]Roy@gmail.com[/TD]
[TD]Nestle[/TD]
[TD]Setup[/TD]
[TD]Vince[/TD]
[TD]Roy[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Vince@gmail.com[/TD]
[/TR]
[TR]
[TD="align: right"]86796[/TD]
[TD]Johny[/TD]
[TD="align: right"]05/04/2019[/TD]
[TD]RS[/TD]
[TD]Roy@ymail.com[/TD]
[TD]Nestle[/TD]
[TD]Setup[/TD]
[TD]Vince[/TD]
[TD]Roy[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Vince@gmail.com




[/TD]
[/TR]
</tbody>[/TABLE]

Workbook 4

[TABLE="width: 898"]
<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD]ID[/TD]
[TD]From[/TD]
[TD]Date[/TD]
[TD]Country[/TD]
[TD]Generic mailid[/TD]
[TD]Contracter name[/TD]
[TD]Activity[/TD]
[TD]User[/TD]
[TD]Reporter[/TD]
[TD]Form[/TD]
[TD]TAT[/TD]
[TD]Days[/TD]
[TD]Form[/TD]
[TD]TAT[/TD]
[TD]Days[/TD]
[TD]User email[/TD]
[/TR]
[TR]
[TD="align: right"]8675[/TD]
[TD]John[/TD]
[TD="align: right"]05/03/2019[/TD]
[TD]ENG[/TD]
[TD]Roy@gmail.com[/TD]
[TD]Muller[/TD]
[TD]Setup[/TD]
[TD]Wright[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Wight@gmail.com[/TD]
[/TR]
[TR]
[TD="align: right"]86752[/TD]
[TD]John[/TD]
[TD="align: right"]05/03/2019[/TD]
[TD]ENG[/TD]
[TD]Roy@ymail.com[/TD]
[TD]Muller[/TD]
[TD]Setup[/TD]
[TD]Wright[/TD]
[TD]Ronak[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Wight@gmail.com[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Try this.
Change Sheet1 by the name of your sheet.

Code:
Sub Sending_emails()
  Dim c As Range, sh As Worksheet, Ky As Variant, dam As Variant, dict As Object
  Dim correo As String, lr As Long, wFile As String
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set sh = Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
  lr = sh.Range("E" & Rows.Count).End(xlUp).Row
  sh.Range("AA:AA").ClearContents
  For Each c In sh.Range("E2", sh.Range("E" & Rows.Count).End(xlUp))
    sh.Range("AA" & c.Row) = c & sh.Range("H" & c.Row) & sh.Range("P" & c.Row)
  Next
  
  Set dict = CreateObject("scripting.dictionary")
  For Each c In sh.Range("AA2", sh.Range("AA" & Rows.Count).End(xlUp))
    dict.Item(c.Value) = sh.Range("P" & c.Row)
  Next
  For Each Ky In dict.Keys
    correo = dict(Ky)
    sh.Range("A1:AA" & lr).AutoFilter Columns("AA").Column, Ky
    Workbooks.Add
    sh.AutoFilter.Range.EntireRow.Copy Range("A1")
    Range("AA:AA").ClearContents
    wFile = ThisWorkbook.Path & "\book.xlsx"
    ActiveWorkbook.SaveAs wFile
    ActiveWorkbook.Close False
    Set dam = CreateObject("Outlook.Application").CreateItem(0)
    dam.To = correo
    dam.Subject = "Please go through attached file."
    dam.Body = "body"
    dam.Attachments.Add wFile
    dam.Display   'use .Send to send
  Next Ky
  sh.ShowAllData
  MsgBox "Done"
End Sub
 
Upvote 0
Hi Dante,

I have tried the code and im receiving error message as runtime error 1004 stating we cant do that at a merged cell from sh.Range("AA:AA").ClearContents and it continues

In my sheet the data starts from A3 and in A1, A2 there are few merged cells. Could could be amended?

Also i want to enter CC email address as well and in column "E" will have different generic email address can outlook use that as from email address?

Can outlook also pick signature?

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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