Skip to content

Instantly share code, notes, and snippets.

@furyutei
Last active April 22, 2020 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furyutei/fd4cae7aa5b6488fd1ebe1f314c62616 to your computer and use it in GitHub Desktop.
Save furyutei/fd4cae7aa5b6488fd1ebe1f314c62616 to your computer and use it in GitHub Desktop.
Excelで特定行の削除/Undoを検知する試み
Option Explicit
Private Const ConfigSheetName = "config"
Private WatchCellConfig As Range
Private WatchCellDeleted As Boolean
Private Enum ChangeKind
Normal = 0
Deleted = 1
Undo = 2
End Enum
Private Sub Worksheet_Activate()
Call UpdateWatchCell
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call UpdateWatchCell
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If UpdateWatchCell() Then Exit Sub
Dim ChgKind As ChangeKind: ChgKind = GetChangeKind()
If ChgKind = ChangeKind.Deleted Then
Debug.Print "* DELETED *", WatchCellConfig.Formula
ElseIf ChgKind = ChangeKind.Undo Then
Debug.Print "* UNDO *", WatchCellConfig.Formula, WatchCellConfig.Value
Else
Debug.Print "Normal", WatchCellConfig.Formula, WatchCellConfig.Value
End If
End Sub
Private Function GetChangeKind() As ChangeKind
GetChangeKind = ChangeKind.Normal
If WatchCellDeleted Then
If Not IsReferenceError(WatchCellConfig) Then
WatchCellDeleted = False
GetChangeKind = ChangeKind.Undo
End If
Else
If IsReferenceError(WatchCellConfig) Then
WatchCellDeleted = True
GetChangeKind = ChangeKind.Deleted
End If
End If
End Function
Private Function UpdateWatchCell() As Boolean
If WatchCellConfig Is Nothing Then
Set WatchCellConfig = Worksheets(ConfigSheetName).Range("A1")
WatchCellDeleted = IIf(IsReferenceError(WatchCellConfig), True, False)
UpdateWatchCell = True
Exit Function
End If
End Function
Private Function IsReferenceError(TargetRange As Range) As Boolean
If TargetRange.Formula Like "*![#]REF!" Then IsReferenceError = True
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment