オブジェクトに以下をコピーしてThisWorkbook
、特定の変更を監視します。この場合、数値を別の数値に増やします。
注意: Workbook-SheetChange
andWorkbook-SheetSelectionChange
をアンダースコアに置き換える必要があります。例: Markdown コードWorkbook_SheetChange
でWorkbook_SheetSelectionChange
アンダースコアがエスケープされます。
Option Explicit
Dim varPreviousValue As Variant ' required for IsThisMyChange() . This should be made more unique since it's in the global space.
Private Sub Workbook-SheetChange(ByVal Sh As Object, ByVal Target As Range)
' required for IsThisMyChange()
IsThisMyChange Sh, Target
End Sub
Private Sub Workbook-SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
' This implements and awful way of accessing the previous value via a global.
' not pretty but required for IsThisMyChange()
varPreviousValue = Target.Cells(1, 1).Value ' NB: This is used so that if a Merged set of cells if referenced only the first cell is used
End Sub
Private Sub IsThisMyChange(Sh As Object, Target As Range)
Dim isMyChange As Boolean
Dim dblValue As Double
Dim dblPreviousValue As Double
isMyChange = False
' Simple catch all. If either number cant be expressed as doubles, then exit.
On Error GoTo ErrorHandler
dblValue = CDbl(Target.Value)
dblPreviousValue = CDbl(varPreviousValue)
On Error GoTo 0 ' This turns off "On Error" statements in VBA.
If dblValue > dblPreviousValue Then
isMyChange = True
End If
If isMyChange Then
MsgBox ("You've increased the value of " & Target.Address)
End If
' end of normal execution
Exit Sub
ErrorHandler:
' Do nothing much.
Exit Sub
End Sub
これに基づいて別のワークブックを変更したい場合は、最初にワークブックが既に開いているかどうかを確認することを検討します...または、すべての変更をまとめて一度に実行できるソリューションを設計することをお勧めします。このスプレッドシートを聞いていることに基づいて、別のスプレッドシートを継続的に変更するのは苦痛になる可能性があります。