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

Tópicos relacionados a códigos VBA, gravação de macros, etc.
#56852
Pessoal, por favor, uma ajuda:

Tenho um código que roda no evento Worksheet_Change, funciona perfeitamente, entretanto, sempre que seleciono uma linha (ou mais) ou então várias células e mando excluir (ou delete) recebo o erro" Erro em tempo de execução '13': Tipos incompatíveis". Existe alguma função que posso adicionar para evitar esse erro?
#56854
Coloque o código aqui, por favor.
#56877
Caro babdallas, segue o código
Após uma modificação que eu fiz no código, o erro passou a ocorrer apenas quando eu seleciono varias células e aperto o botão Delete
___________________________________________________________________________________________________
Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False

If Target.Column = 16 Then

Dim DOC As String
DOC = Target.Value

If Len(DOC) = 11 Then
Target.Value = Mid(DOC, 1, 3) & "." & Mid(DOC, 4, 3) & "." & Mid(DOC, 7, 3) & "-" & Mid(DOC, 10, 2)
Cells(Target.Row, 10) = Date & " " & Time

ElseIf Len(DOC) = 14 Then
Target.Value = Mid(DOC, 1, 2) & "." & Mid(DOC, 3, 3) & "." & Mid(DOC, 6, 3) & "/" & Mid(DOC, 9, 4) & "-" & Mid(DOC, 13, 2)
Cells(Target.Row, 10) = Date & " " & Time
Else
Target.Value = Null


End If

ElseIf Target.Column = 26 Or Target.Column = 32 Then
If Len(Target.Value) = 10 Then
Target.Value = "(" & Mid(Target.Value, 1, 2) & ")" & Mid(Target.Value, 3, 4) & "-" & Mid(Target.Value, 7, 10)
Cells(Target.Row, 9) = Date & " " & Time
Else
Target.Value = Null
End If

ElseIf Target.Column = 27 Or Target.Column = 33 Then
If Len(Target.Value) = 11 Then
Target.Value = "(" & Mid(Target.Value, 1, 2) & ")" & Mid(Target.Value, 3, 5) & "-" & Mid(Target.Value, 8, 10)
Cells(Target.Row, 9) = Date & " " & Time
Else
Target.Value = Null
End If

ElseIf Target.Column = 35 Then
If Len(Target.Value) = 8 Then
Target.Value = Mid(Target.Value, 1, 5) & "-" & Mid(Target.Value, 6, 8)
Cells(Target.Row, 9) = Date & " " & Time
Else
Target.Value = Null
End If

ElseIf Target.Column = 29 Then
If Target.Value = "Sim" Then
Cells(Target.Row, 30) = ActiveCell.Offset(0, -5).Value
Cells(Target.Row, 31) = ActiveCell.Offset(0, -4).Value
Cells(Target.Row, 32) = ActiveCell.Offset(0, -3).Value
Cells(Target.Row, 33) = ActiveCell.Offset(0, -2).Value
Cells(Target.Row, 34) = ActiveCell.Offset(0, -1).Value
Cells(Target.Row, 9) = Date & " " & Time
End If

ElseIf Target.Column = 14 Or Target.Column = 15 Or Target.Column > 17 Then
Cells(Target.Row, 9) = Date & " " & Time

End If

Application.EnableEvents = True

End Sub
_______________________________________________________________________________________________________
Obrigado pela ajuda!
#56882
Quando você seleciona mais de uma célula ou linhas, o Target possui mais de um Valor (ou seja, é um vetor de valores). Logo dará erro na linha que vc atribui o valor de Target para a variável DOC.
A questão é, quando você seleciona mais de uma célula, o que você quer que ocorra: que a rotina faça um loop por todas as célula e execute esta rotina para cada uma ou vai aplicar a rotina somente para a célula ativa?
#56952
Teste isso, por favor
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    
    If Target.Column = 16 Then
    
        Dim DOC As String
        
        If Target.Cells.Count > 1 Then
            DOC = Target.Value2(1, 1)
        Else
            DOC = Target.Value2
        End If
        
        If Len(DOC) = 11 Then
            Target.Value = Mid(DOC, 1, 3) & "." & Mid(DOC, 4, 3) & "." & _
                            Mid(DOC, 7, 3) & "-" & Mid(DOC, 10, 2)
            Cells(Target.Row, 10) = Date & " " & Time
        
        ElseIf Len(DOC) = 14 Then
            Target.Value = Mid(DOC, 1, 2) & "." & Mid(DOC, 3, 3) & "." & _
                            Mid(DOC, 6, 3) & "/" & Mid(DOC, 9, 4) & "-" & Mid(DOC, 13, 2)
            Cells(Target.Row, 10) = Date & " " & Time
        Else
            Target.Value = Null
        End If
        
    ElseIf Target.Column = 26 Or Target.Column = 32 Then
        If Len(Target.Value) = 10 Then
            Target.Value = "(" & Mid(Target.Value, 1, 2) & ")" & _
                            Mid(Target.Value, 3, 4) & "-" & Mid(Target.Value, 7, 10)
            Cells(Target.Row, 9) = Date & " " & Time
        Else
            Target.Value = Null
        End If
        
    ElseIf Target.Column = 27 Or Target.Column = 33 Then
        If Len(Target.Value) = 11 Then
            Target.Value = "(" & Mid(Target.Value, 1, 2) & ")" & _
                            Mid(Target.Value, 3, 5) & "-" & Mid(Target.Value, 8, 10)
            Cells(Target.Row, 9) = Date & " " & Time
        Else
            Target.Value = Null
        End If
        
    ElseIf Target.Column = 35 Then
        If Len(Target.Value) = 8 Then
            Target.Value = Mid(Target.Value, 1, 5) & "-" & Mid(Target.Value, 6, 8)
            Cells(Target.Row, 9) = Date & " " & Time
        Else
            Target.Value = Null
        End If
        
    ElseIf Target.Column = 29 Then
        If Target.Value = "Sim" Then
            Cells(Target.Row, 30) = ActiveCell.Offset(0, -5).Value
            Cells(Target.Row, 31) = ActiveCell.Offset(0, -4).Value
            Cells(Target.Row, 32) = ActiveCell.Offset(0, -3).Value
            Cells(Target.Row, 33) = ActiveCell.Offset(0, -2).Value
            Cells(Target.Row, 34) = ActiveCell.Offset(0, -1).Value
            Cells(Target.Row, 9) = Date & " " & Time
        End If
        
    ElseIf Target.Column = 14 Or Target.Column = 15 Or Target.Column > 17 Then
        Cells(Target.Row, 9) = Date & " " & Time
    End If
    
    Application.EnableEvents = True
End Sub
#56953
Acrescente a linha em vermelho, conforme abaixo.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count>1 Then Exit Sub
Application.EnableEvents = False
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