A rotina abaixo após se digitar os dados em uma determinada planilha aciona-se a macro e a mesma corre a coluna B e D, verificando se na coluna B tem a palavra "Anulação", se sim converte o valor da coluna D para negativo, até ai tudo bem, funciona perfeitamente.
Essa Rotina funciona como deveria.
Código: Selecionar todos
'Converte Valor Positivo para Negativo conforme palavra Anulação
Sub PositivoNegativo()
Dim i As Integer
i = 2 'Linha 2
While Cells(i, 2).Value <> "" 'Verifica Linha 2 + Coluna B
If Cells(i, 2).Value = "Anulação" Then 'Se valor = Anulação
If Cells(i, 4).Value < 0 Then 'Verifica se o valor já é negativo COLUNA D
Else
Cells(i, 4).Value = Cells(i, 4).Value * -1 'Altera o Valor de D em Negativo
Cells(i, 4).Font.ColorIndex = 3 'Altera cor da fonte para vermelho
End If
End If
i = i + 1
Wend
End Sub
Na intenção de automatizar sem precisar ficar acionando a rotina, adaptei para ser usada no Evento Worksheet_Change da seguinte forma :
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal target As Range)
Application.EnableEvents = False
If Not Intersect(target, Range("D3:D30")) Is Nothing Then 'Até C30 é só modificar
If target.Offset(0, -2) = "Anulação" Then 'Se Na Coluna A tiver Anulação
target = target.Value * -1 'Transforma Valor de C de Positivo em Negativo
target.Font.ColorIndex = 3 'Formata a fonte em Vermelho
End If
End If
Application.EnableEvents = True
End Sub
Código: Selecionar todos
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("D3:D150")) Is Nothing Then 'Até C30 é só modificar
Call PositivoNegativo
End If
Application.EnableEvents = True
End Sub
[REMOVIDO]
Desde já agradeço a atenção.
abraços