Jump to content

VBA - Como resolver loop infinito


JOSE L. BONFIM

Recommended Posts

Olá, estou com um loop infinito e não sei como resolver.

Estou usando o evento change para saber se a célula foi altera.
Em seguida vou passando por algumas Sub's.
Ocorre que no célula data entro em loop sem sair.

01 - Imagem
https://www.dropbox.com/s/csfh2iuwurzwuqy/vba_change.png?dl=0

02 - Codigo WorkSheet_Change
 

Option Explicit

Private Sub WorkSheet_Change(ByVal Target As Range)

    If Target.Address = "$C$2" Then
        Range("$C$2").NumberFormat = "dd/mm/yyyy"
        Valida_Data
    End If
    
    If Target.Address = "$E$2" Then
        Range("$E$2").NumberFormat = "@"
        Digita_Hora
    End If
  
End Sub

03 - Modulos
 

Option Explicit
Dim str_Data_Inicio As String
Dim str_Hora_Inicio As String


Sub Valida_Data()

    If IsDate(Range("$C$2")) = True Then
      str_Data_Inicio = Mid(Range("$C$2"), 7, 4) & "-" & Mid(Range("$C$2"), 4, 2) & "-" & Mid(Range("$C$2"), 1, 2)
     ' Range("$E$2").ClearContents
     ' Range("$E$2").Select
     Digita_Hora
    Else
        MsgBox ("data inválida")
        Range("$C$2").Select
        
    End If
End Sub

Sub Digita_Hora()
      ' Range("$E$2").ClearContents
       Range("$E$2").Select
      
      If Len(Range("$E$2")) < 4 Or Len(Range("$E$2")) > 5 Then
         MsgBox ("Hora inválida, digite hora com 4 caracteres"), vbExclamation
        ' Range("$E$2").ClearContents
         Range("$E$2").Select
      Else
         MsgBox ("vou validar hora")
         Valida_Hora
   End If
End Sub

Sub Valida_Hora()

   ' str_Hora_Inicio As String
 '   MsgBox (str_Hora)
    If IsNumeric(Left(Range("$E$2"), 2)) And IsNumeric(Right(Range("$E$2"), 2)) Then

        str_Hora_Inicio = Left(Range("$E$2"), 2) & ":" & Right(Range("$E$2"), 2)
        str_Data_Inicio = str_Data_Inicio & " " & str_Hora_Inicio
        
        If Len(str_Hora_Inicio) = 5 Then
            Range("$E$2") = str_Hora_Inicio
        End If
        
    Else
            MsgBox ("Hora inválida, digita_hora")
            Digita_Hora
            Range("$E$2").ClearContents
    End If
    
  
End Sub

Sub Criar_Plan()

MsgBox ("Criar Plan")


End Sub

Agradeço a quem puder me auxiliar.
José Bonfim

Link to comment
Share on other sites

No limite podes ter sempre um contador e se passares lá, por exemplo 100 vezes, sais fora.
No entanto isso cheira-me a um problema mais profundo de lógica.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.