Excel Macro to select the latest date

Closed
Sachin - May 9, 2010 at 12:08 AM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - May 9, 2010 at 08:29 AM
Hi,

I have a excel sheet with 6 sheets (1,2,3,4,5,6). The macro for the excel sheet would select all the rows with status 'In Progress' in column P across all the sheets and will send out an email.

There are two problems I am facing here:
1. When an email is created the rows for each sheet is displayed one below the other. However the row/column size is not standard. Certain cells are big in size and hence the representation is not looking good.

2. In all the sheets column Q = Remarks. Here each cell has data based on dates.
for example
[2/2/2010 - DAta data data
1/1/2020 - Data1 data1 data1]

What code should i write so that only the topmost date and the related data is selected from a particular cell?


PLease find the macro below for your reference:

Option Explicit

Sub Mail_Sheet_Outlook_Body()
' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2010
Dim ProgressRNG As Range
Dim ProgressRNG2 As Range
Dim ProgressRNG3 As Range
Dim ProgressRNG4 As Range
Dim ProgressRNG5 As Range
Dim ProgressRNG6 As Range

Dim OutApp As Object
Dim OutMail As Object
Dim LastRow As Long

Application.EnableEvents = False
Application.ScreenUpdating = False

With Sheets("1")
.AutoFilterMode = False
LastRow = .Cells.Find("*", Cells(.Rows.Count, .Columns.Count), _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("P:P").AutoFilter Field:=1, Criteria1:="In Progress"
Set ProgressRNG = .Range("A1:A" & LastRow) _
.SpecialCells(xlCellTypeVisible).EntireRow
End With

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

With Sheets("2")
.AutoFilterMode = False
LastRow = .Cells.Find("*", Cells(.Rows.Count, .Columns.Count), _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("P:P").AutoFilter Field:=1, Criteria1:="In Progress"
Set ProgressRNG2 = .Range("A1:A" & LastRow) _
.SpecialCells(xlCellTypeVisible).EntireRow
End With

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

With Sheets("3")
.AutoFilterMode = False
LastRow = .Cells.Find("*", Cells(.Rows.Count, .Columns.Count), _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("P:P").AutoFilter Field:=1, Criteria1:="In Progress"
Set ProgressRNG3 = .Range("A1:A" & LastRow) _
.SpecialCells(xlCellTypeVisible).EntireRow
End With

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

With Sheets("4")
.AutoFilterMode = False
LastRow = .Cells.Find("*", Cells(.Rows.Count, .Columns.Count), _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("P:P").AutoFilter Field:=1, Criteria1:="In Progress"
Set ProgressRNG4 = .Range("A1:A" & LastRow) _
.SpecialCells(xlCellTypeVisible).EntireRow
End With

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

With Sheets("5")
.AutoFilterMode = False
LastRow = .Cells.Find("*", Cells(.Rows.Count, .Columns.Count), _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("P:P").AutoFilter Field:=1, Criteria1:="In Progress"
Set ProgressRNG5 = .Range("A1:A" & LastRow) _
.SpecialCells(xlCellTypeVisible).EntireRow
End With

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

With Sheets("6")
.AutoFilterMode = False
LastRow = .Cells.Find("*", Cells(.Rows.Count, .Columns.Count), _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("P:P").AutoFilter Field:=1, Criteria1:="In Progress"
Set ProgressRNG6 = .Range("A1:A" & LastRow) _
.SpecialCells(xlCellTypeVisible).EntireRow
End With

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

On Error Resume Next
With OutMail
.To = "@gmail.in"
.CC = ""
.BCC = ""
.Subject = "Status as on "
.HTMLBody = "1" & RangetoHTML(ProgressRNG) & "<br>" & "2" & RangetoHTML(ProgressRNG2) & "<br>" & "<br>" & "3" & RangetoHTML(ProgressRNG3) & "<br>" & "3" & RangetoHTML(ProgressRNG4) & "<br>" & "5" & RangetoHTML(ProgressRNG5) & "<br>" & "6" & RangetoHTML(ProgressRNG6)

.Display 'or use .Send
End With
On Error GoTo 0

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

Sheets("1").AutoFilterMode = False
Sheets("2").AutoFilterMode = False
Sheets("3").AutoFilterMode = False
Sheets("4").AutoFilterMode = False
Sheets("5").AutoFilterMode = False
Sheets("6").AutoFilterMode = False
Set OutMail = Nothing
Set OutApp = Nothing
Set ProgressRNG = Nothing
Set ProgressRNG2 = Nothing
Set ProgressRNG3 = Nothing
Set ProgressRNG4 = Nothing
Set ProgressRNG5 = Nothing
Set ProgressRNG6 = Nothing
End Sub

2 responses

rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
May 9, 2010 at 06:04 AM
For both question answer is that modify your routine RangetoHTML. Have it first write to a temp sheet all the data. Once all the data is completed, then copy the data for email Body. In this way you can sort the data and also you can have presentation style resolved too.
0
ya thats where i am stuck.. can u help me with this...?
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
May 9, 2010 at 06:11 AM
Could you please upload a sample file with sample data and macros etc on some shared site like https://authentification.site and post back here the link to allow better understanding of how it is now and how you foresee.
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
May 9, 2010 at 08:29 AM
Sachin, based on your sample the date in Q are like this

1/1/2010: Param is
2/2/2010: New Person Assigned

In your question, you said the dates would be like
2/2/2010: New Person Assigned
1/1/2010: Param is


Which is correct ?
0