I'm trying to loop through a column, while checking if cell in row is empty and if today's date minus date in table are higher or the same as 7. If requirements are met send email.
Private Sub Workbook_Open()
Dim myApp As Outlook.Application, mymail As Outlook.MailItem
Dim datum1 As Date
Dim datum2 As Long
Dim danasnji1 As Date
Dim danasnji2 As Long
Dim x As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lastrow
If IsEmpty(Cells(x, 10).Value) Then
x = x + 1
Else
datum1 = Cells(x, 10).Value
datum2 = datum1
danasnji1 = Date
danasnji2 = danasnji1
If danasnji2 - datum2 >= 7 Then
Set myApp = New Outlook.Application
Set mymail = myApp.CreateItem(olMailItem)
mymail.To = "examlemail@mail"
With mymail
.Subject = "Test"
.body = "Body Code"
.Send
End With
Cells(x, 10).Font.Color = RGB(255, 0, 0)
End If
End If
Next
Set myApp = Nothing
Set mymail = Nothing
End Sub
Picture of Excel table column:
This code sends email but the loop skips some cells. There should be 5 emails, but it sends 3. Cells that worked are coloured red.