Função VBA do Excel para atualizar o valor das células + = valor de célula acima

1

Comopossofazercomque,seeuinseriralgonointervalodecélulasB3:E3,ovalorseráoqueeuinserir,maisovalordacélulaacima.

exemplo:Seeuinserir20nacélulaC3,quandoeuclicaremretornarouclicarfora,ovalorseráatualizadopara40.

Atéagoraeuseiqueprecisoteralgoparecidocomisso:

PrivateSubWorksheet_SelectionChange(ByValTargetAsRange)Range("B3:E3").Value = Range("B2:E2").Value

End Sub

E sei que preciso acessar o valor de deslocamento de alguma forma, mas o VBA não é meu strong e não consigo pronunciar corretamente essa pergunta.

    
por selectDistinct 30.05.2017 / 22:52

1 resposta

1

Você precisará adicionar uma carga de verificação de erros para isso, mas aqui está o esqueleto:

Private Sub Worksheet_Change(ByVal Target As Range)

  ' If we're not in the range of interest
  ' do nothing.
  If Intersect(Target, Me.Range("B2:E2")) Is Nothing Then
    Exit Sub
  End If
  ' We don't want this event to fire itself!
  Application.EnableEvents = False
  ' Now add the row above (rowOffset = -1) to the value.
  Target.Value = Target.Value + Target.Offset(-1, 0).Value
  ' Don't forget to re-enable events, otherwise there'll
  ' be no more VBA events fired.
  Application.EnableEvents = True

End Sub
    
por 30.05.2017 / 23:10