Suponha que eu queira espelhar a célula Sheet1.[A4]
com a célula Sheet2.[B7]
, usaria o seguinte:
Folha1
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range) 'Sheet1 Module
If Not Intersect(Target, [A4]) Is Nothing Then 'Update [B7] only when [A4] is updated
MirrorCells Worksheets("Sheet2").[B7], Target
End If
End Sub
Folha2
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range) 'Sheet2 Module
If Not Intersect(Target, [B7]) Is Nothing Then 'Update [A4] only when [B7] is updated
MirrorCells Worksheets("Sheet1").[A4], Target
End If
End Sub
Module1 (módulo padrão)
Option Explicit
Public Sub MirrorCells(ByRef oldVal As Range, ByRef newVal As Range)
If newVal.CountLarge = 1 Then 'Check that updated range is 1 cell (paste operations)
If Not IsError(oldVal) And Not IsError(newVal) Then 'Ignore errors
If oldVal <> newVal Then 'If oldVal needs to be updated
Application.EnableEvents = False 'Stop all events
oldVal.Value = newVal.Value 'Update it (triggers a new event)
Application.EnableEvents = True 'Turn events back on
End If
End If
End If
End Sub
Run-time error '9'
é gerado porque você não tem uma planilha chamada exatamente "Sheet2"
Verifique se há espaços extras no nome da guia, ex "Sheet 2"
, ou "Sheet2 "
ou " Sheet2"