Bom dia!!
Mude o formato da célula para geral com a linha
Código: Selecionar todosPrivate Sub Worksheet_Change(ByVal Target As Range)
Dim datestr As String
On Error GoTo EndMacro
Range("D12").NumberFormat = "General" 'Mude o formato da célula
If Application.Intersect(Target, Range("D12")) Is Nothing Then
'O intervalo corresponde a 1 linha na coluna "D", munde se necessário
Exit Sub
End If
If Target.Cells.Count > 1 Then
Exit Sub
End If
If Target.Value = "" Then
Exit Sub
End If
Application.EnableEvents = False
With Target
If .HasFormula = False Then
'Digite valores para datas coerentes, respeitando o prazo dos dias (até 31 dias), mêses (até 12 mêses)
'Se digitar 4 caracteres, corresponderá a dias e mêses, exemplo: 1502 - será 15/02/2005
'Para os anos , digite com os dois algarismos, exemplo: 2005 - digite: 05
Select Case Len(.Value)
Case 3 'para, 103 = 1/03/2005
datestr = Left(.Value, 1) & "/" & Right(.Value, 2)
Case 4 'para, 1505 = 15/05/2005
datestr = Left(.Value, 2) & "/" & Right(.Value, 2)
Case 5 'para 10705 = 1/07/2005
datestr = Left(.Value, 1) & "/" & Mid(.Value, 2, 2) & "/" & Right(.Value, 2)
Case 6 'para , 231005 = 23/10/2005
datestr = Left(.Value, 2) & "/" & Mid(.Value, 3, 2) & "/" & Right(.Value, 2)
'lembro que a formatação poderá ser alterada nas células conforme necessidade
Case Else
Err.Raise 0
End Select
.Value = DateValue(datestr)
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "A data digitada não é válida!"
Application.EnableEvents = True
End Sub
Att
Espero ter Ajudado.
Se a mensagem foi util Favor
Clicar na mãozinha.
Quando necessário, lembre se de marcar o tópico como
[RESOLVIDO].
Tenha um bom dia
