• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Send Excel Table in Mail body based on Filtered data and corresponding email in a separate sheet|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Send Excel Table in Mail body based on Filtered data and corresponding email in a separate sheet|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosSend Excel Table in Mail body based…
sp_PrintTopic sp_TopicIcon
Send Excel Table in Mail body based on Filtered data and corresponding email in a separate sheet
Avatar
Marko Meglic
Member
Members
Level 0
Forum Posts: 13
Member Since:
March 22, 2023
sp_UserOfflineSmall Offline
1
April 7, 2023 - 11:15 pm
sp_Permalink sp_Print

Hi Guys

I need help with this one because is a mix of some tasks.

I need a Sen Mail Macro for this Table.

A Column values (DSP) are the one to be filtered and the Email Addresses are in a separate Sheet (DSP Emails) where the DSP are listed with the emails 

CC is in sheet C10

 

the Table should be in Email Body with some text and signature.

some Macros are already made but with no success.

Avatar
Hans Hallebeek
the Netherlands
Member
Members


Trusted Members
Level 0
Forum Posts: 182
Member Since:
October 17, 2018
sp_UserOfflineSmall Offline
2
April 8, 2023 - 4:41 pm
sp_Permalink sp_Print sp_EditHistory

Hi, I dn't have Outloo installed to test it so I do not know what errors you get

I did modify this code and maybe it can help you

 

[code]

Public Sub mailK()

Dim Wks As Worksheet
Dim OutMail As Object
Dim OutApp As Object
Dim myRng As Range
Dim list As Object
Dim item As Variant
Dim LastRow As Long
Dim uniquesArray()
Dim Dest As String
Dim strbody
Dim fRange As Range
Set Wks = ThisWorkbook.Sheets("FICO DATA")

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

With Wks
For Each item In .Range("A2", .Range("A" & .Rows.Count).End(xlUp))

Next
End With

On Error Resume Next
Wks.ShowAllData
Err.Clear
Set fRange = Wks.Range("A1:L" & Range("A" & Rows.Count).End(xlUp).Row)
For Each item In list
Wks.Range("A1:L" & Range("A" & Rows.Count).End(xlUp).Row).AutoFilter Field:=1
Wks.Range("A1:L" & Range("A" & Rows.Count).End(xlUp).Row).AutoFilter Field:=1, Criteria1:=item

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next

LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set myRng = fRange.SpecialCells(xlCellTypeVisible)
' Set myRng = Wks.Range("A1:L" & LastRow).SpecialCells(xlCellTypeVisible)
If Err.Number = 0 Then
Dest = WorksheetFunction.VLookup(Range("A2").Value, Worksheets("DSP Emails").Range("B:C"), 2)
strbody = "Guten Morgen DSPs," & vbNewLine & vbNewLine & _
"Anbei die heutige FICO Auswertung." & vbNewLine & vbNewLine & _
"mit freundlichen Grüßen," & vbNewLine & vbNewLine & _
"your ORM TEAM"

With OutMail
.To = Dest
.CC = Worksheets("DSP Emails").Range("C10")
.BCC = ""
.Subject = "FICO Auswertung"
.HTMLBody = strbody & RangetoHTML(myRng)
.Display
'.Send
End With
End If
Err.Clear
Next item

On Error Resume Next
Wks.ShowAllData
On Error GoTo 0

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

End Sub
[/code]

Avatar
Marko Meglic
Member
Members
Level 0
Forum Posts: 13
Member Since:
March 22, 2023
sp_UserOfflineSmall Offline
3
April 8, 2023 - 9:39 pm
sp_Permalink sp_Print

Thanks for the help but that did not solve my problem of filtering data to a specific in A column.

it has to be filtered and then send the filtered values as a mail body to the designated DSP in A Column

Avatar
Hans Hallebeek
the Netherlands
Member
Members


Trusted Members
Level 0
Forum Posts: 182
Member Since:
October 17, 2018
sp_UserOfflineSmall Offline
4
April 9, 2023 - 6:06 pm
sp_Permalink sp_Print sp_EditHistory

Slightly modified code

Public Sub mailK()

Dim Wks As Worksheet
Dim OutMail As Object
Dim OutApp As Object
Dim myRng As Range
Dim list As Object
Dim item As Range
Dim LastRow As Long
Dim uniquesArray()
Dim Dest As String
Dim strbody
Dim fRange As Range
Dim tbl As ListObject
Set Wks = ThisWorkbook.Sheets("FICO DATA")
Set tbl = Wks.ListObjects("FICOTable")
With Application
.EnableEvents = False
.ScreenUpdating = False
End With

With Wks
For Each item In .Range("A2", .Range("A" & .Rows.Count).End(xlUp))

Next
End With

On Error Resume Next
Wks.ShowAllData
Err.Clear
Set fRange = tbl.DataBodyRange
For Each item In tbl.ListColumns(1).DataBodyRange
fRange.AutoFilter Field:=1
fRange.AutoFilter Field:=1, Criteria1:=item

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next

' LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set myRng = fRange.SpecialCells(xlCellTypeVisible)
' Set myRng = Wks.Range("A1:L" & LastRow).SpecialCells(xlCellTypeVisible)
If Err.Number = 0 Then
Dest = WorksheetFunction.VLookup(item.Text, Worksheets("DSP Emails").Range("B:C"), 2)
strbody = "Guten Morgen DSPs," & vbNewLine & vbNewLine & _
"Anbei die heutige FICO Auswertung." & vbNewLine & vbNewLine & _
"mit freundlichen Grüßen," & vbNewLine & vbNewLine & _
"your ORM TEAM"

With OutMail
.To = Dest
.CC = Worksheets("DSP Emails").Range("C10")
.BCC = ""
.Subject = "FICO Auswertung"
.HTMLBody = strbody & RangetoHTML(myRng)
.Display
'.Send
End With
End If
Err.Clear
Next item

On Error Resume Next
Wks.ShowAllData
On Error GoTo 0

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

End Sub

Avatar
Hans Hallebeek
the Netherlands
Member
Members


Trusted Members
Level 0
Forum Posts: 182
Member Since:
October 17, 2018
sp_UserOfflineSmall Offline
5
April 9, 2023 - 7:09 pm
sp_Permalink sp_Print sp_EditHistory

I updated the file you attached and have adapted the code a little.

Chandoo's idea is great but when you work with Tables you can use a better approach.

Like I mentioned I do not have Outlook installed but the process seems to work as designed

Happy Easter

 

PS the second attachment is correct

Avatar
Hans Hallebeek
the Netherlands
Member
Members


Trusted Members
Level 0
Forum Posts: 182
Member Since:
October 17, 2018
sp_UserOfflineSmall Offline
6
April 12, 2023 - 4:13 pm
sp_Permalink sp_Print

I would appreciate it if you could find the time to tell us if the solution/option helped you.

Avatar
Marko Meglic
Member
Members
Level 0
Forum Posts: 13
Member Since:
March 22, 2023
sp_UserOfflineSmall Offline
7
April 12, 2023 - 4:58 pm
sp_Permalink sp_Print sp_EditHistory

Public Sub Geht()

Dim Wks As Worksheet
Dim OutMail As Object
Dim OutApp As Object
Dim myRng As Range
Dim list As Object
Dim item As Range
Dim LastRow As Long
Dim uniquesArray()
Dim Dest As String
Dim strbody
Dim fRange As Range
Dim tbl As ListObject
Set Wks = ThisWorkbook.Sheets("FICO DATA")
Set tbl = Wks.ListObjects("FICOTable")

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

With Wks
For Each item In .Range("A1", .Range("A" & .Rows.Count).End(xlUp))

Next
End With

On Error Resume Next
Wks.ShowAllData
Err.Clear
Set fRange = tbl.DataBodyRange
For Each item In tbl.ListColumns(1).DataBodyRange
fRange.AutoFilter Field:=1
fRange.AutoFilter Field:=1, Criteria1:=item

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next

' LastRow = Cells(Rows.Count, "A").End(xlUp).Row
' Set myRng = fRange.SpecialCells(xlCellTypeVisible)
Set myRng = Sheets("FICO DATA").Range("A1:L4").SpecialCells(xlCellTypeVisible)
' Set myRng = Wks.Range("A1:L" & LastRow).SpecialCells(xlCellTypeVisible)
If Err.Number = 0 Then
Dest = WorksheetFunction.VLookup(item.Text, Worksheets("DSP Emails").Range("B:C"), 2, False)
strbody = "Guten Morgen DSPs," & vbNewLine & vbNewLine & _
"Anbei die heutige FICO Auswertung." & vbNewLine & vbNewLine & _
"mit freundlichen Grüßen," & vbNewLine & vbNewLine & _
"your ORM TEAM"

With OutMail
.To = Dest
.CC = Worksheets("DSP Emails").Range("C10")
.BCC = ""
.Subject = "FICO Auswertung"
.HTMLBody = strbody & RangetoHTML(myRng) & vbNewLine & Signature
.Display
'.Send
End With
End If
Err.Clear
Next item

On Error Resume Next
Wks.ShowAllData
On Error GoTo 0

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

End Sub

Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With

'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With

'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")

'Close TempWB
TempWB.Close savechanges:=False

'Delete the htm file we used in this function
Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function

 

That is what i got out with a little modification from my side now every DSP in Row A who is filtered is then send a email based on the email List.

Thank you guys for helping me on this topic!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

Your are THE BEST

Avatar
Hans Hallebeek
the Netherlands
Member
Members


Trusted Members
Level 0
Forum Posts: 182
Member Since:
October 17, 2018
sp_UserOfflineSmall Offline
8
April 13, 2023 - 4:07 pm
sp_Permalink sp_Print

Good to read.

What's the code for the Signature?

Avatar
Marko Meglic
Member
Members
Level 0
Forum Posts: 13
Member Since:
March 22, 2023
sp_UserOfflineSmall Offline
9
April 14, 2023 - 10:37 pm
sp_Permalink sp_Print

The signature does not work. but i am ok with it 😀

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Saliha Mohamed, David Eva
Guest(s) 10
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 880
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 237
Jessica Stewart: 219
A.Maurizio: 213
Aye Mu: 201
jaryszek: 183
Newest Members:
Ashley Hughes
Herbie Key
Trevor Pindling
Stevan Kanjo
Erin Sheldon
Nikita Bhatia
Sheilah Taylor
Clare Webber
David Jenssen
Dominic Brosnahan
Forum Stats:
Groups: 3
Forums: 24
Topics: 6526
Posts: 28576

 

Member Stats:
Guest Posters: 49
Members: 32810
Moderators: 2
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.