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.
#53517
Olá. Existe algum código vba que substitua um caractere de uma célula por outro? Por exemplo: quero que quando eu digite " : " este seja substituído automaticamente por " = " e que essa substituição seja feita assim que eu selecionar qualquer outra célula, e que conserve todo o resto que foi escrito na célula.

Obrigado.
#53519
Veja se ajuda.
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lngTamanho      As Long
    Dim lngLoop         As Long
    Dim vrtTexto()      As Variant
    Dim strCaract       As String
    
    lngTamanho = VBA.Len(Target.Value)
    
    If lngTamanho = 0 Then Exit Sub
    
    ReDim vrtTexto(1 To lngTamanho) As Variant
    
    Application.EnableEvents = False
    For lngLoop = 1 To lngTamanho
        strCaract = VBA.Mid(Target.Value, lngLoop, 1)
        If Asc(strCaract) = VBA.Asc(";") Then
            vrtTexto(lngLoop) = "="
        Else
            vrtTexto(lngLoop) = strCaract
        End If
    Next lngLoop
    
    Target.Value = VBA.Join(vrtTexto, "")
    
    Erase vrtTexto
    Application.EnableEvents = True
End Sub
Você não está autorizado a ver ou baixar esse anexo.
#53534
babdallas escreveu:Veja se ajuda.
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lngTamanho      As Long
    Dim lngLoop         As Long
    Dim vrtTexto()      As Variant
    Dim strCaract       As String
    
    lngTamanho = VBA.Len(Target.Value)
    
    If lngTamanho = 0 Then Exit Sub
    
    ReDim vrtTexto(1 To lngTamanho) As Variant
    
    Application.EnableEvents = False
    For lngLoop = 1 To lngTamanho
        strCaract = VBA.Mid(Target.Value, lngLoop, 1)
        If Asc(strCaract) = VBA.Asc(";") Then
            vrtTexto(lngLoop) = "="
        Else
            vrtTexto(lngLoop) = strCaract
        End If
    Next lngLoop
    
    Target.Value = VBA.Join(vrtTexto, "")
    
    Erase vrtTexto
    Application.EnableEvents = True
End Sub

Esse código é quase perfeito pra mim, fiz um teste aqui e realmente funciona... Mas como faço pra aplicar ele apenas num range de células específico? Por exemplo (A2:B3).
#53550
Tente isso.
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lngTamanho      As Long
    Dim lngLoop         As Long
    Dim vrtTexto()      As Variant
    Dim strCaract       As String
    
    lngTamanho = VBA.Len(Target.Value)
    
    If lngTamanho = 0 Then Exit Sub
    
    ReDim vrtTexto(1 To lngTamanho) As Variant
    
    Application.EnableEvents = False
    if not Application.Intersect(Target, Me.Range("A2:B3")) is nothing then
        For lngLoop = 1 To lngTamanho
             strCaract = VBA.Mid(Target.Value, lngLoop, 1)
            If Asc(strCaract) = VBA.Asc(";") Then
                vrtTexto(lngLoop) = "="
            Else
                vrtTexto(lngLoop) = strCaract
            End If
        Next lngLoop
    
         Target.Value = VBA.Join(vrtTexto, "")
    
        Erase vrtTexto
    end if
    Application.EnableEvents = True
End Sub
#53558
Venho trazer a solução para minha questão. Um colega chamado Anderson, de um outro fórum, me deu uma força e consegui usar o código que ele me propôs, e então resolvi meu problema. Segue o código Vba.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False

Me.Range("A3").Value = Replace(Me.Range("A3").Value, ":", "=")
Me.Range("A4").Value = Replace(Me.Range("A4").Value, ":", "=")
Me.Range("B3").Value = Replace(Me.Range("B3").Value, ":", "=")
Me.Range("B4").Value = Replace(Me.Range("B4").Value, ":", "=")

Application.EnableEvents = True
End Sub
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