Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Dúvidas sobre cálculos, funções simples e aninhadas, fórmulas matriciais, etc.
  • Avatar do usuário
#61150
Bom dia

Estou com problema no seguinte módulo

Private Sub Workbook_Open()

Dim datanasc, nome, cnhdata, grad, aba, i As Variant
i = 1
For Each aba In ThisWorkbook.Sheets
Sheets(i).Select
If aba.Name <> "EFETIVO CIA" And aba.Name <> "RECADASTRAMENTO" And aba.Name <> "AR" And aba.Name <> "VALIDADE" And aba.Name <> "VALIDADE 2" And aba.Name <> "AL" Then
If Cells(12, 8) - Date < 30 Then
cnhdata = Cells(12, 8)
nome = Cells(2, 4)
grad = Cells(3, 4)
MsgBox "CNH DO " & grad & " " & nome & " VENCEU/VENCERÁ EM " & cnhdata, vbExclamation, "ATENÇÃO"
End If
End If
i = i + 1
Next

End Sub

A célula (12,8) contém DATA ou quando não há data o caractere "-" (no caso anulando a célula apenas para não ficar vazia). Quando o módulo encontra o primeiro caractere "-", ele para de funcionar. Sou aprendiz ainda em Excel então nesse caso necessito da ajuda dos srs.
Editado pela última vez por henriquerosa em 18 Dez 2020 às 12:51, em um total de 1 vez.
#61151
Olá e bem vindo.

O problema está a ocorrer, pois o "-" não é uma data e quando ele vai tentar fazer a conta Cells(12,8) - Date dá erro.
Pois você apenas consegue calcular data com data. Não sei se me expliquei bem. Caso não tenha percebido a razão do erro diga que tento explicar melhor.

Para resolver o seu erro vai ter que fazer um IF antes para verificar se o valor da célula é data ou não.
Para isso vai utilizar: IsDate

Ou seja o seu código vai ficar assim (Alterações a verde):

Private Sub Workbook_Open()

Dim datanasc, nome, cnhdata, grad, aba, i As Variant
i = 1
For Each aba In ThisWorkbook.Sheets
Sheets(i).Select
If aba.Name <> "EFETIVO CIA" And aba.Name <> "RECADASTRAMENTO" And aba.Name <> "AR" And aba.Name <> "VALIDADE" And aba.Name <> "VALIDADE 2" And aba.Name <> "AL" Then

If IsDate(Cells(12, 8)) Then
If Cells(12, 8) - Date < 30 Then
cnhdata = Cells(12, 8)
nome = Cells(2, 4)
grad = Cells(3, 4)
MsgBox "CNH DO " & grad & " " & nome & " VENCEU/VENCERÁ EM " & cnhdata, vbExclamation, "ATENÇÃO"
End If

End If
End If
i = i + 1
Next

End Sub
henriquerosa agradeceu por isso
#61152
AfonsoMira escreveu: 18 Dez 2020 às 11:16 Olá e bem vindo.

O problema está a ocorrer, pois o "-" não é uma data e quando ele vai tentar fazer a conta Cells(12,8) - Date dá erro.
Pois você apenas consegue calcular data com data. Não sei se me expliquei bem. Caso não tenha percebido a razão do erro diga que tento explicar melhor.

Para resolver o seu erro vai ter que fazer um IF antes para verificar se o valor da célula é data ou não.
Para isso vai utilizar: IsDate

Ou seja o seu código vai ficar assim (Alterações a verde):

Private Sub Workbook_Open()

Dim datanasc, nome, cnhdata, grad, aba, i As Variant
i = 1
For Each aba In ThisWorkbook.Sheets
Sheets(i).Select
If aba.Name <> "EFETIVO CIA" And aba.Name <> "RECADASTRAMENTO" And aba.Name <> "AR" And aba.Name <> "VALIDADE" And aba.Name <> "VALIDADE 2" And aba.Name <> "AL" Then

If IsDate(Cells(12, 8)) Then
If Cells(12, 8) - Date < 30 Then
cnhdata = Cells(12, 8)
nome = Cells(2, 4)
grad = Cells(3, 4)
MsgBox "CNH DO " & grad & " " & nome & " VENCEU/VENCERÁ EM " & cnhdata, vbExclamation, "ATENÇÃO"
End If

End If
End If
i = i + 1
Next

End Sub
Obrigado, me ajudou muito. Desconhecia esse comando.
AfonsoMira agradeceu por isso
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord