Query on declaring 3 separate ranges to be copied onto an email

4dam5mith

New Member
Joined
Aug 3, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am trying to copy data (with determinate columns and indeterminate rows) from 3 sheets on the same worksheet onto an email.

The issue I am having is the declaration of range for the 3 separate sheets, sheet A will always have columns A1 to J1, sheet C and sheet R will always have columns A1 to I1, whereas for all 3 sheets the rows may vary on a daily basis - please refer to the below for a snippet of the code:

Dim arng As Range
Dim crng As Range
Dim rrng As Range

Set arng = Worksheets("A").Range("$A$1:$J$1" & Cells(Rows.Count, "A").End(xlUp).Row)
Set crng = Worksheets("C").Range("$A$1:$I$1" & Cells(Rows.Count, "A").End(xlUp).Row)
Set rrng = Worksheets("R").Range("$A$1:$I$1" & Cells(Rows.Count, "A").End(xlUp).Row)

For the above:
  • Inclusive of the header on the top row; Sheet A has data from A1 to J6, however it copies the table from A1 to J12
  • Inclusive of the header on the top row; Sheet C has data from A1 to I2, however it copies the table from A1 to I12
  • Inclusive of the header on the top row; Sheet R has data from A1 to I16, however it copies the table from A1 to I13
Can I check if there is a better way of selecting all the cells on each respective worksheet, or whether there is a workaround on my code so that it selects and copies all the data within each worksheet - not more and not less?

Thanks
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Please see the below for the full code for your reference

Sub SendUpdateEmail()

Dim outlook As Object
Dim newEmail As Object
Dim EmailTo As String
Dim EmailCC As String
Dim UpdateDate As String
Dim strSig As String
Dim arng As Range
Dim crng As Range
Dim rrng As Range



Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
EmailTo = "test@gmail.com"
EmailCC = "test@gmail.com"
UpdateDate = Format(Date, "YYYY-MM-DD")
Set arng = Worksheets("A").Range("$A$1:$J$1" & Cells(Rows.Count, "A").End(xlUp).Row)
Set crng = Worksheets("C").Range("$A$1:$I$1" & Cells(Rows.Count, "A").End(xlUp).Row)
Set rrng = Worksheets("R").Range("$A$1:$I$1" & Cells(Rows.Count, "A").End(xlUp).Row)

With newEmail
.To = EmailTo
.CC = EmailCC
.BCC = ""
.Subject = "XXXXXXX " + UpdateDate + " YYYYYYYYY"
.Display
strSig = .HTMLBody

.HTMLBody = "Morning Team," & "<br>" & "<br>" & "<b> XXXXXXXXX </b>" + RangetoHTML(arng) & "<br>" & "<br>" & "<b> YYYYYYYY </b>" + RangetoHTML(crng) & "<br>" & "<br>" & "<b> ZZZZZZZ </b>" + RangetoHTML(rrng)

.Display

End With

Set newEmail = Nothing
Set outlook = Nothing

End Sub


Function RangetoHTML(rng As Range)
' By Ron de Bruin.
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
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,375
Members
449,098
Latest member
Jabe

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