vlookup match data and send an email

mali10020

New Member
Joined
Oct 15, 2021
Messages
17
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Hello everyone! I hope you are doing well

I'm working on a project and I need help please!!

I have 2 sheets in my workbook Report and email.. The sheet "report" have all the data and sheet "email" have all the users email..

I would like the data shown in the report to go to the appropriate email associated with the cost centerI want this code to go to the

Email sheet


Report sheet
Device nameMACLocationModelcost center
CGS00595653C:CD:88:E1:59:755TiPHone 13
10259​
CGS0084533C:CD:88:E1:59:OP5YiPHone 13
10280​
CGI00564653C:CD:88:E1:59:756GiPHone 13
10280​
CGS00365483C:CD:88:E1:59:005BiPHone 13
10280​
CGS00464653C:CD:88:E1:59:32:465NiPHone 13
10286​
CGS007895323C:CD:88:E1:59:091RiPHone 13
10286​
CGS766153C:CD:88:E1:59:859UiPHone 13
10289​
CGS003245693C:C45:D:88:E1:596WiPHone 13
10291​
CGS0064633C:CD:88:E1:59:HY5TiPHone 13
10298​
CGI75643C:CD:88:E1:59:VT1AiPHone 13
10298​
IPH0076523C:CD:88:E1:59:127OiPHone 13
10298​
IPH12542333C:CD:88:E1:59:769WiPHone 13
10298​
IPH009863C:CD:88:E1:59:DT6LiPHone 13
10298​

VBA Code:
Sub Email_Report()
    Dim Source As Range
    Dim Dest As Workbook
    Dim wb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim OutApp As Object
    Dim OutMail As Object

    Set Source = Nothing
    On Error Resume Next
    Set Source = Worksheets("Report").Range("A:I").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
        Exit Sub
    End If

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

    Set wb = ActiveWorkbook
    Set Dest = Workbooks.Add(xlWBATWorksheet)

    Source.Copy
    With Dest.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial Paste:=xlPasteValues
        .Cells(1).PasteSpecial Paste:=xlPasteFormats
        .Cells(1).Select
        Application.CutCopyMode = False
    End With

    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Selection of " & wb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    If Val(Application.Version) < 12 Then
        'You use Excel 97-2003
        FileExtStr = ".xls": FileFormatNum = -4143
    Else
        'You use Excel 2007-2016
        FileExtStr = ".xlsx": FileFormatNum = 51
    End If

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

    With Dest
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .To = ""
            .CC = ""
            .BCC = ""
            .Subject = "TEST Daily Mobile Devices Report"
            .Body = "Good evening! "
            .Attachments.Add Dest.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\Report.txt")
            .Send   'or use .Display
        End With
        On Error GoTo 0
        .Close SaveChanges:=False
    End With

    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 
Last edited by a moderator:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Is what I'm trying to do is possible.. Please assist if you can.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,213,529
Messages
6,114,155
Members
448,554
Latest member
Gleisner2

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