I'm developing a script in vba , so it saves all incoming attachments with new e-mail, with extension xlsx . It verifies that it is the right attachment (through a code that the worksheet will have), copies the entire contents of that workbook (if it is correct) to another and automatically sends a reply to the sender from email. All of this is working perfectly.
In column G, of this spreadsheet that arrives by e-mail, has a list with the months (that represents the person's birthday month). If the request comes more than two months in advance, I would like the automatic response to have an observation for that request.
For example: This is December. Requests can only be made for the months of December, January and February. So if, in some line of the spreadsheet, you have a request for months other than those, the text of the automatic response would have an extra stretch.
That's the problem. I can not think of a logic that does this check correctly.
Edit: Below I wrote a code snippet very close to what I'm using (the logic is the same, I just do not have it here):
Dim mesHoje As Integer, mesSolicitacao As Integer
...
For i=6 To ultimaLinha
If Plan1.Cells(i, "G") = "Janeiro" Then
mesSolicitacao = 1
ElseIf Plan1.Cells(i, "G") = "Fevereiro" Then
mesSolicitacao = 2
...
ElseIf Plan1.Cells(i, "G") = "Dezembro" Then
mesSolicitacao = 12
End If
mesHoje = Month(Date)
If Abs((mesHoje + 2) - mesSolicitacao) <= 2 Then '<~ Esse if que eu não estou conseguindo pensar em uma forma de fazer, do jeito que eu quero. Talvez com um if só não da.
'Rotina para solicitação feita dentro do prazo
Else
'Rotina para solicitação feita fora de prazo
End If
Next
Edit: I think I found the solution (I did not do all the tests, but of the many I did, in excel, they worked). I just changed if from the code below. But would you have a "cleaner" way of writing this passage?
If mesHoje < 11 Then
If ((mesHoje + 2) - mesSolicitacao) <= 2 And ((mesHoje + 2) - mesSolicitacao) > (-1) Then
'Rotina para solicitação feita dentro do prazo
Else
'Rotina para solicitação feita fora do prazo
End If
ElseIf mesHoje = 11 Then
If (mesHoje = 11 And mesSolicitacao = 11) Or (mesHoje = 11 And mesSolicitacao = 12) Or (mesHoje = 11 And mesSolicitacao = 1) Then
'Rotina para solicitação feita dentro do prazo
Else
'Rotina para solicitação feita fora do prazo
End If
ElseIf mesHoje = 12 Then
If (mesHoje = 12 And mesSolicitacao = 12) Or (mesHoje = 12 And mesSolicitacao = 1) Or (mesHoje = 12 And mesSolicitacao = 2) Then
'Rotina para solicitação feita dentro do prazo
Else
'Rotina para solicitação feita fora do prazo
End If
End If