Elliottj2121
Board Regular
- Joined
- Apr 15, 2021
- Messages
- 56
- Office Version
- 365
- 2019
- Platform
- Windows
Hello Everyone!
I have a macro written and it works exactly how I would like it to work. However, I am newer at writing macros and my skills are very limited so I go on this forum and others to piece together bits of code here and there to make what I need to do work. I have the macro code listed below and it seems to run a little slow. Could someone be my "editor" and clean it up so it is more efficient and a little more polished?
I am taking a worksheet in excel with customer data and creating a table in an outlook email for each customer to email a summary of their invoices. I have example data below also. Thank you in advance!
I have a macro written and it works exactly how I would like it to work. However, I am newer at writing macros and my skills are very limited so I go on this forum and others to piece together bits of code here and there to make what I need to do work. I have the macro code listed below and it seems to run a little slow. Could someone be my "editor" and clean it up so it is more efficient and a little more polished?
I am taking a worksheet in excel with customer data and creating a table in an outlook email for each customer to email a summary of their invoices. I have example data below also. Thank you in advance!
VBA Code:
Sub EMAIL_10th_PAYORS()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
ProperCNCN
i45Email
Condense
Save_List
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Private Sub ProperCNCN()
Dim wsProper As Worksheet
Set wsProper = ActiveWorkbook.Worksheets(1)
Dim lProperLastRow As Long, c As Range, n As Range, t As Range
Dim CustNameRange As Range, ContNameRange As Range
Dim lCustNameRange As String, lContNameRange As String
Dim LR As Long
LR = ProperLastRow(wsProper)
lProperLastRow = ProperLastRow(wsProper)
Set CustNameRange = Range("B2:B" & lProperLastRow)
Set ContNameRange = Range("H2:H" & lProperLastRow)
For Each c In CustNameRange
c.Value = Application.WorksheetFunction.Proper(c.Value)
Next c
For Each n In ContNameRange
n.Value = Application.WorksheetFunction.Proper(n.Value)
Next n
With wsProper
.Cells.NumberFormat = "General"
.Columns("I:S").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
.Columns(8).TextToColumns Destination:=Range("H1"), DataType:=xlDelimited, _
TextQualifier:=xlNone, ConsecutiveDelimiter:=True, Tab:=False, Semicolon _
:=False, Comma:=False, Space:=True, Other:=False, FieldInfo:=Array( _
Array(1, 1), Array(2, 9), Array(3, 9), Array(4, 9)), TrailingMinusNumbers:=True
.Columns("I:S").Delete Shift:=xlToLeft
.Columns("H:H").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
.Range("H1").FormulaR1C1 = "Total"
.Range("H2").FormulaR1C1 = _
"=IF(RC[-7]<>R[1]C[-7],SUMIFS(R1C[-2]:RC[-2],R1C[-7]:RC[-7],RC[-7]),"""")"
.Range("H2").AutoFill Range("H2:H" & LR)
.Columns("H").NumberFormat = "General"
.Columns("F").Style = "Currency"
.Columns("H").Style = "Currency"
.Columns.AutoFit
End With
End Sub
Private Sub i45Email()
Set rng = Range(Range("J2"), Range("J" & Rows.Count).End(xlUp))
x = rng.Rows.Count
tableHdr = "<table border=1 style=border-collapse:collapse><tr><th width=101 style='width:76.1pt;padding:.75pt .75pt .75pt .75pt;background-color:#1C6EA4'><p align=center style='text-align:center'><font color='white'><b>" & Range("C1").Value & "</b></font></th>" _
& "<th width=101 style='width:76.1pt;padding:.75pt .75pt .75pt .75pt;background-color:#1C6EA4'><p align=center style='text-align:center'><font color='white'><b>" & Range("D1").Value & "</b></font></th>" _
& "<th width=101 style='width:76.1pt;padding:.75pt .75pt .75pt .75pt;background-color:#1C6EA4'><p align=center style='text-align:center'><font color='white'><b>" & Range("E1").Value & "</b></font></th>" _
& "<th width=101 style='width:76.1pt;padding:.75pt .75pt .75pt .75pt;background-color:#1C6EA4'><p align=center style='text-align:center'><font color='white'><b>" & Range("F1").Value & "</b></font></th>" _
& "<th width=101 style='width:76.1pt;padding:.75pt .75pt .75pt .75pt;background-color:#1C6EA4'><p align=center style='text-align:center'><font color='white'><b>" & Range("G1").Value & "</b></font></th>" _
& "<th width=101 style='width:76.1pt;padding:.75pt .75pt .75pt .75pt;background-color:#1C6EA4'><p align=center style='text-align:center'><font color='white'><b>" & Range("H1").Value & "</b></font></th>" _
For Each Cell In rng
If Cell.Value <> "" Then
If Not Cell.Offset(0, 1).Value = "yes" Then
NmeRow = Cell.Row
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Para1 = "We will be processing a payment for the invoices listed below on 10th of this month or the next business day after the 10th."
MailTo = Cell.Value
MailSubject = "Monthly payment for" & " " & Cell.Offset(0, -8).Value
filename = Cell.Offset(0, -6).Value & "_" & Format(Date, "mm") + 1 & ".10." & Format(Date, "yy")
Greeting = "<p><span style='font-size:12.0pt;font-family:'Times New Roman',serif'>Hello" & " " & Cell.Offset(0, -1).Value & "," & "</span></p>"
Message = "<p><span style='font-size:12.0pt;font-family:'Times New Roman',serif'>" & Para1 & "</p>" & "</span></p>"
MailBody = "<tr>" _
& "<td align=center style='text-align:center'>" & Cell.Offset(0, -7).Value & "</td>" _
& "<td align=center style='text-align:center'>" & Cell.Offset(0, -6).Value & "</td>" _
& "<td align=center style='text-align:center'>" & Cell.Offset(0, -5).Value & "</td>" _
& "<td align=center style='text-align:center'>" & Cell.Offset(0, -4).Value & "</td>" _
& "<td align=center style='text-align:center'>" & Cell.Offset(0, -3).Value & "</td>" _
& "<td span class='dollars' align=center style='text-align:center'>" & Cell.Offset(0, -2).Value & "</td>" _
& "</tr>"
For Each dwn In rng.Offset(NmeRow - 1, 0)
If dwn.Value = Cell.Value Then
AddRow = "<tr>" _
& "<td align=center style='text-align:center'>" & dwn.Offset(0, -7).Value & "</td>" _
& "<td align=center style='text-align:center'>" & dwn.Offset(0, -6).Value & "</td>" _
& "<td align=center style='text-align:center'>" & dwn.Offset(0, -5).Value & "</td>" _
& "<td align=center style='text-align:center'>" & dwn.Offset(0, -4).Value & "</td>" _
& "<td align=center style='text-align:center'>" & dwn.Offset(0, -3).Value & "</td>" _
& "<td span class='dollars' align=center style='text-align:center'>" & dwn.Offset(0, -2).Value & "</td>" _
& "</tr>"
dwn.Offset(0, 1).Value = "yes"
MailBody = MailBody & AddRow 'column A
End If
AddRow = ""
Next
With OutMail
.To = MailTo
.Subject = MailSubject
.HTMLBody = "<html>" & Logo & Greeting & Message & tableHdr & MailBody & "</table>" & Break & "</html>"
.Save
'.Close
'.Display
'.Send
End With
Cell.Offset(0, 1).Value = "yes"
End If
End If
MailTo = ""
MailSubject = ""
MailBody = ""
Next
Range("K2:K" & x).ClearContents
End Sub
Private Sub Condense()
Dim r As Long
Dim TR As Range
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets(1)
LR = ProperLastRow(ws)
Set TR = Range("H2:H" & LR)
Columns("H:H").Copy
Columns("H:H").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
For r = TR.Cells.Count To 1 Step -1
With TR.Cells(r)
If .Value = "" Then
.EntireRow.Delete
End If
End With
Next r
Columns("C:G").Delete
Columns("D:G").Delete
End Sub
Private Sub Save_List()
Dim filename As String, path As String, FNS As String
filename = Format(Date, "mm") & ".10_LIST"
path = "U:\Company Shares\DC Office Shares\Credit\Credit File\ACH Payments\Pending ACH\"
ActiveWorkbook.SaveAs filename:=path & filename & ".xlsx", FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close
End Sub
Function ProperLastRow(sh As Worksheet) As Variant
On Error Resume Next
ProperLastRow = sh.Cells.Find(What:="*", _
lookat:=xlWhole, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
End Function
Customer # | Customer | Inv# | Date | Age | Amount | PO | Contact | |
100 | Alpha Company | 1109971 | 6/7/2024 | 18 | 1854.55 | Alpha8 | Joe | alphaco@anymail.com |
100 | Alpha Company | 1109972 | 6/7/2024 | 18 | 1423.79 | Alpha8 | Joe | alphaco@anymail.com |
100 | Alpha Company | 1109973 | 6/7/2024 | 18 | 1187.33 | Alpha8 | Joe | alphaco@anymail.com |
100 | Alpha Company | 1110254 | 6/7/2024 | 18 | 324.82 | Alpha8 | Joe | alphaco@anymail.com |
101 | Beta Inc. | 1110306 | 6/7/2024 | 18 | 30.24 | Beta9 | Mike | Beta@anymail.com |
101 | Beta Inc. | 1110706 | 6/11/2024 | 14 | 181.44 | Beta10 | Mike | Beta@anymail.com |
102 | Gamma LLC | 1109968 | 6/12/2024 | 13 | 10015.19 | Gamma88 | Paul | Gamma@anymail.com |
102 | Gamma LLC | 1112290 | 6/13/2024 | 12 | 2451.8 | Gamma88 | Paul | Gamma@anymail.com |
102 | Gamma LLC | 2416060 | 6/3/2024 | 22 | 238.09 | Gamma101 | Paul | Gamma@anymail.com |
102 | Gamma LLC | 2416644 | 6/4/2024 | 21 | 602.47 | Gamma101 | Paul | Gamma@anymail.com |
103 | Delta LLP | 2416063 | 6/10/2024 | 15 | 853.17 | 456123 | John | Delta@anymail.com |