VBA - How to string two commands into one macro

Florida1510

New Member
Joined
Mar 13, 2020
Messages
35
Office Version
  1. 2010
Platform
  1. Windows
Folks,

I need some assistance I'm trying to add a second request to my existing macro to either screenshot or copy a range of cells from the ACC Data Input tab - Sheet1 (Range A1:D21). I would like for the range to be placed right under the wording "High Usage SIMs - Average SIM Usage - X.XXMB" which is part of the HTMLBody of this code. For the life of me I can't get it to work. Any help would be truly appreciated.

Sub sendEmail()

' ******************************** Variable Declaration ********************************
' Dim Req_id As Integer, nextReq_id As Integer, x As Integer, y As Integer, rownumber As Integer, _
' colnumber As Integer, finalRow As Integer, finalColumn As Integer, errCounter As Integer

' Dim TestFreq As String, month As String, pvt As PivotTable
Dim RangeToCopy As Range
Dim wsACC As Worksheet, wsContacts As Worksheet
Dim OutApp As Object, OutMail As Object
Dim lRow As Long
Dim PEmailId As String, CompanyName As String, SEmailId As String, CycleClose As String, Now As String
Dim FilesToOpen

' ******************************** Setting variables ********************************

Set wsACC = ThisWorkbook.Sheets("ACC")
Set wsSheet1 = ThisWorkbook.Sheets("ACC Data Input")
Set wsContacts = ThisWorkbook.Sheets("Contacts")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.createitem(0)


' ******************************** processing start ********************************

'Setting width for month column to avoid it being displayed as #### in outlook

CompanyName = wsContacts.Range("A2")
PEmailId = wsContacts.Range("B2")
SEmailId = wsContacts.Range("C2")
CycleClose = wsContacts.Range("D2")



With wsACC
.Activate

lRow = Cells(Rows.Count, 1).End(xlUp).Row

Set RangeToCopy = .Range("A1:L" & lRow + 1)
Columns("A:L").AutoFit


' ********************************** Prepare the email reminder **********************************
Set OutMail = OutApp.createitem(0)
With OutMail
.Display 'or use .Send
.To = PEmailId
.CC = SEmailId
' .BCC = ""
' .attachments.Add

.Subject = CompanyName & " ESS Weekly Update " & Date
.HTMLBody = "<font size=""3""> <p><b>Hello All" & ",<p>" & _
"Below is the ESS Weekly Update for the cycle closing on the " & Now & CycleClose & ". <p>" & _
"Attached is the report for the high data usage devices for the week.<p>" & _
"The table below reflects the current cycle to date usage through " & Date & " and current rate plan allocations.</font>" & RangetoHTML(RangeToCopy) & _
"<p><font color=""grey""><em> * Usage will continue to be monitored through the cycle.</em></font>" & _
"<font size=""3""> <p><b>High Usage SIMs - Average SIM Usage - X.XXMB" & "<p>" & "<font size=""3""> <p><b>Maintenance" & "<p>" & _
"<font size=""3""> <p><b>Billing" & "<p>" & _
"<font size=""3""> <p><b>Items of Note" & "<p>" & _
"<font size=""3""><p> Kindly advise if you have any questions or need further information.</p></font></b>" & OutMail.HTMLBody


End With

End With


ExitHandler:
Application.ScreenUpdating = True
Set OutMail = Nothing
Set OutApp = Nothing
Exit Sub


End Sub
 

Attachments

  • Screenshot of data.png
    Screenshot of data.png
    205.8 KB · Views: 6

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.
You already use RangetoHTML, you have just to reuse that function for the desired range...

Try replacing this line:
Rich (BB code):
"<font size=""3""> <p><b>High Usage SIMs - Average SIM Usage - X.XXMB" & "<p>" & "<font size=""3""> <p><b>Maintenance" & "<p>" & _

with
Code:
"<font size=""3""> <p><b>High Usage SIMs - Average SIM Usage - X.XXMB" & "<p>" &<br> & RangetoHTML(wsSheet1.Range("A1:D21") _
& "<font size=""3""> <p><b>Maintenance" & "<p>" & _
The macro then continues with the line "<font size=""3""> <p><b>Billing" & "<p>" & _

Bye
 
Upvote 0
Anthony,

Thank you for the quick response but I'm still getting a compliance Sytax error error on the code. Any thoughts?

.Subject = CompanyName & " ESS Weekly Update " & Date
.HTMLBody = "<font size=""3""> <p><b>Hello All" & ",<p>" & _
"Below is the ESS Weekly Update for the cycle closing on the " & Now & CycleClose & ". <p>" & _
"Attached is the report for the high data usage devices for the week.<p>" & _
"The table below reflects the current cycle to date usage through " & Date & " and current rate plan allocations.</font>" & RangetoHTML(RangeToCopy) & _
"<p><font color=""grey""><em> * Usage will continue to be monitored through the cycle.</em></font>" & _
"<font size=""3""> <p><b>High Usage SIMs - Average SIM Usage - X.XXMB" & "<p>" & ",<br> & RangetoHTML(wsSheet1.Range("A1:D21") _
& "<font size=""3""> <p><b>Maintenance" & "<p>" & _
"<font size=""3""> <p><b>Billing" & "<p>" & _
"<font size=""3""> <p><b>Items of Note" & "<p>" & _
"<font size=""3""><p> Kindly advise if you have any questions or need further information.</p></font></b>" & OutMail.HTMLBody
 
Upvote 0
Rather than using such a long multi-multi lines command you should split it in smaller chunks; for example:
VBA Code:
.HTMLBody = "<font size=""3""> <p><b>Hello All" & ",<p>"                                                              'First chunk                                    
.HTMLBody = .HTMLBody & "Below is the ESS Weekly Update for the cycle closing on the " & Now & CycleClose & ". <p>"   'Second chunk
.HTMLBody = .HTMLBody & "Attached is the report for the high data usage devices for the week.<p>"                     'Third chunk
'etc etc

This should be easier to check

Anyway, I missed a small but significant "doublequote" in my code:
Rich (BB code):
.HTMLBody = .HTMLBody & "<font size=""3""> <p><b>High Usage SIMs - Average SIM Usage - X.XXMB" & "<p>" & ",<br>" & RangetoHTML(wsSheet1.Range("A1:D21")
'other chunks
The corrected portion is the one in Red & bold

Bye
 
Upvote 0
Anthony,

Thank you again I have done everything you have recommended but on one line I'm now getting the an Syntex error on line:

.HTMLBody = .HTMLBody & "<font size=""3""> <p><b>High Usage SIMs - Average SIM Usage - X.XXMB" & "<p>" & ",<br>" & RangetoHTML(wsSheet1.Range("A1:D21")

This is what the code now looks like:

.Subject = CompanyName & " ESS Weekly Update " & Date
.HTMLBody = "<font size=""3""> <p><b>Hello All" & ",<p>"
.HTMLBody = .HTMLBody & "Below is the ESS Weekly Update for the cycle closing on the " & Now & CycleClose & ". <p>"
.HTMLBody = .HTMLBody & "Attached is the report for the high data usage devices for the week.<p>"
.HTMLBody = .HTMLBody & "The table below reflects the current cycle to date usage through " & Date & " and current rate plan allocations.</font>" & RangetoHTML(RangeToCopy)
.HTMLBody = .HTMLBody & "<p><font color=""grey""><em> * Usage will continue to be monitored through the cycle.</em></font>"
.HTMLBody = .HTMLBody & "<font size=""3""> <p><b>High Usage SIMs - Average SIM Usage - X.XXMB" & "<p>" & ",<br>" & RangetoHTML(wsSheet1.Range("A1:D21")
.HTMLBody = .HTMLBody & "<font size=""3""> <p><b>Maintenance" & "<p>"
.HTMLBody = .HTMLBody & "<font size=""3""> <p><b>Billing" & "<p>"
.HTMLBody = .HTMLBody & "<font size=""3""> <p><b>Items of Note" & "<p>"
.HTMLBody = .HTMLBody & "<font size=""3""><p> Kindly advise if you have any questions or need further information.</p></font></b>" & OutMail.HTMLBody


End With

Any thoughts

Owen
 
Upvote 0
:mad: Grrrr...
That line need a final closing parenthesis ")": RangetoHTML(wsSheet1.Range("A1:D21"))
 
Upvote 0
I realize only now that you start with something in the HTMLBody (a signature?).
So you have to "compose" your body using a string variable, and then you compile HTMLBody:
VBA Code:
myMess = "<font size=""3""> <p><b>Hello All" & ",<p>"
myMess = myMess & "Below is the ESS Weekly Update for the cycle closing on the " & Now & CycleClose & ". <p>"
'etc
'etc
myMess = myMess & "<font size=""3""><p> Kindly advise if you have any questions or need further information.</p></font></b>"
.HTMLBody = myMess & .HTMLBody
 
Upvote 0
Anthony,

I'm still getting that same Sntax Error on line:

myMess = myMess & "<font size=""3""> <p><b>High Usage SIMs - Average SIM Usage - X.XXMB" & "<p>" & ",<br>" & RangetoHTML(wsSheet1.Range("A1:D21")

This is what the code looks like now:

.Subject = CompanyName & " ESS Weekly Update " & Date
myMess = "<font size=""3""> <p><b>Hello All" & ",<p>"
myMess = myMess & "Below is the ESS Weekly Update for the cycle closing on the " & Now & CycleClose & ". <p>"
myMess = myMess & "Attached is the report for the high data usage devices for the week.<p>"
myMess = myMess & "The table below reflects the current cycle to date usage through " & Date & " and current rate plan allocations.</font>" & RangetoHTML(RangeToCopy)
myMess = myMess & "<p><font color=""grey""><em> * Usage will continue to be monitored through the cycle.</em></font>"
myMess = myMess & "<font size=""3""> <p><b>High Usage SIMs - Average SIM Usage - X.XXMB" & "<p>" & ",<br>" & RangetoHTML(wsSheet1.Range("A1:D21")
myMess = myMess & "<font size=""3""> <p><b>Maintenance" & "<p>"
myMess = myMess & "<font size=""3""> <p><b>Billing" & "<p>"
myMess = myMess & "<font size=""3""> <p><b>Items of Note" & "<p>"
myMess = myMess & "<font size=""3""><p> Kindly advise if you have any questions or need further information.</p></font></b>" & OutMail.HTMLBody
 
Upvote 0
Anthony,

You're a Rock Star that worked I can't thank you enough for all your assistance. I have learned a ton tonight.

Have a wonderful evening!!!!!

Owen
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

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