To Add Excel VBA Code to a Workbook / This Workbook Module

  1. Copy the code that you want to use
  2. Select the workbook in which you want to store the code
  3. Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
  4. In the Project Explorer, find your workbook, and open the list of Microsoft Excel Objects
  5. Right-click on the ThisWorkbook object, and choose View Code
  6. Where the cursor is flashing, choose Edit | Paste

Dim sPrev As String
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    sPrev = Sh.Name
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim vFound As Variant, Ws As Worksheet

On Error Resume Next
    Set Ws = ThisWorkbook.Sheets(sPrev)
On Error GoTo 0

If Ws Is Nothing Then
    With ThisWorkbook.Sheets("Example")
        vFound = Application.Match(sPrev, .Range("A:A"), 0)
        If IsNumeric(vFound) Then .Range("A" & vFound).Delete Shift:=xlUp
    End With
End If

End Sub