Dear Frank_AL,
Hope you had very nice vacation. Thank you for remembering me.
Now I am troubling in joining two macros: the one you built in this thread and the other one created by someone in this forum.
At first, I imagined I could join those macros easily, but in real situation, I don't have enough knowledge to do so although learning about my case in many other threads.
As you know, I have 12 monthly sheets in each of which dates are written in row 3 starting from Column E, but saying pair of 2 columns for one date is more correct. For example, Row 3 Column D is blank and Row 3 Column E is for date. Then, Column BN stands for summing up of total frequencies of being late by staff. That's why I mentioned in previous posts that if column BN is 5 times or 6 times, then send email to the relevant staff and his/her supervisors. However, it is not that simple. I just want to send email only to the staffs who are late "today" and reach 5 or 6 times by today. Otherwise, it would count the staffs, to send eamil, who has been late for 5 or 6 times in previous days.
To handle this conditions, I have macros
1) Today macro to show only pair of columns in second column of which today date exists and hide the rest irrelevant pair of columns between Column C and Column BM. So, it is much easy for me to key in late status of the staffs and BN column will count and add up for today.
2) Filtering macro to apply filter on the today date existing column, 2nd column of column pair, in order to filter out blank cells. So, it leaves staffs who are late today in a visible range, Staff Names in Column C, late status in column pair and total late times of the staff in Column BN.
3) Emailing macro, that you have created in this thread, to find the values in column BN, if the value is 5 or 6 times by today, then read the relevant staff names and vlookup in Email Addr sheet to extract email addresses and send email to them.
For these things, I run Today macro separately and it is fine. Filtering macro is working fine too. Where I am in trouble is joing the filtering macro and emailing macro as follow
Code:
Sub FindLateTimes()
Application.ScreenUpdating = False
Dim rng As Excel.Range
Dim tdlatelist As Excel.Range
Dim tdlatelist1 As String
Dim TL As Long
Dim LC As Long
Dim LR As Long
Dim x As Long
Dim sh As Worksheet
Set sh = ActiveSheet
Dim email As Worksheet
Dim i As Long
Dim y As Long
Dim lastrow As Long
Dim emlastrow As Long
Dim currval As String
Dim OutLookApp As Object
Set OutLookApp = CreateObject("OutLook.Application")
Dim MItem As Object
Dim staff As String
Dim staffem As String
Dim svrem As String
Dim emailbody As String
Const olMailItem = 0
Dim lookuptable As Range
With sh
If .AutoFilterMode Then .AutoFilterMode = False
LC = .Cells(3, .Columns.Count).End(xlToLeft).Column
Set rng = .Range("A3:BQ3").Resize(1, LC).Find(What:=Date, LookIn:=xlValues)
If Not rng Is Nothing Then
x = rng.Column
LR = .Cells(.Rows.Count, rng.Column).End(xlUp).Row
Set rng = .Range("A3:BQ3", rng).Resize(LR)
Else
MsgBox "Date not found, macro stopping", vbOKOnly
End
End If
With rng
.AutoFilter
.AutoFilter field:=x, Criteria1:="<>"
.Range("$C$3:$BQ$90").SpecialCells (xlCellTypeVisible)
Set email = Worksheets("Email Addr")
emlastrow = email.Cells(email.Rows.Count, "A").End(xlUp).Row
lastrow = sh.Cells(sh.Rows.Count, "C").End(xlUp).Row
For i = 4 To lastrow
currval = ""
If sh.Range("BN" & i).Value >= 5 Then
staff = sh.Range("C" & i)
For y = 4 To emlastrow
currval = email.Range("A" & y).Value
If currval = staff Then
staffem = email.Range("B" & y).Value
svrem = email.Range("C" & y).Value
GoTo skip
End If
Next y
skip:
If sh.Range("BN" & i).Value = 5 Then
emailbody = email.Range("E4").Value
Else
emailbody = email.Range("E5").Value
End If
Set MItem = OutLookApp.CreateItem(olMailItem)
' On Error GoTo EmailFailed
With MItem
.BodyFormat = 3
.To = staffem & ", " & svrem
.Subject = "Failure to Comply with Attendance Policy"
.HTMLBody = emailbody
.Display
End With
End If
'EmailFailed:
Next i
End Sub
I am really sorry and afraid to annoy you again. I don't know if I should start a new thread for this problem.
In the above code, my problem is that I don't know how to set a new range for the visible range after being filtered and how to pick up BN column within the visible range to find the values (5 and 6). I am working in these days for it with several ways of results by searching online. But fail and fail.
If it is still relevant in this thread, please help me.
I am sorry to bother you again and again.
Best Regards,
Ko Htut