Excel(エクセル) VBA入門:ワークシートのイベント |
Activate | Change | SelectioChange | BeforeDoubleClick |
イベントプロシージャ | 内容 |
Activate | シートがアクティブになったときに発生します。 構文:Private Sub object_Activate() |
Deactivate | シートがアクティブでなくなったときに発生します。 構文:Private Sub object_Deactivate() |
Change | セルが変更された時に発生します。 |
SelectionChange | 選択範囲が変わった時に発生します |
BeforeDoubleClick | セルがダブルクリックされた時に発生します。 |
BeforeRightClick | セルが右クリックされた時に発生します。 |
Calculate | シートが再計算された時に発生します。 |
FollowHyperlink (Excel2000で追加) |
ハイパーリンクがクリックされた時に発生します。 |
PivotTableUpdate | ピボットテーブルが更新された時に発生します。 |
Private Sub Worksheet_Activate() Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1).Select End Sub |
Private Sub Worksheet_Activate() With Worksheets("Sheet2") .Range("A:I").ClearContents Worksheets("Sheet1").Range("A1").CurrentRegion.Copy .Range("A1").PasteSpecial Paste:=xlValues .Range("A1").CurrentRegion.Sort _ Key1:=Range("G1"), _ Order1:=xlDescending, _ Header:=xlYes, _ OrderCustom:=1, _ Orientation:=xlTopToBottom, _ SortMethod:=xlPinYin End With End Sub |
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Row = 1 Or Target.Row > 6 Then Exit Sub If Target.Column <> 3 Then Exit Sub Application.EnableEvents = False Target.Value = Int(Target.Value * 1.05) Application.EnableEvents = True End Sub |
Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Range("C2:C5")) Is Nothing Then Exit Sub Application.EnableEvents = False Target.Value = Int(Target.Value * 1.05) Application.EnableEvents = True End Sub |
Private Sub Worksheet_Change(ByVal Target As Range) Dim myData As Range, c As Range Set myData = Application.Intersect(Target, Range("C:D")) If myData Is Nothing Then Exit Sub For Each c In myData If IsNumeric(Range("C" & c.Row).Value) And IsNumeric(Range("D" & c.Row).Value) Then Range("E" & c.Row).Value = Int(Range("C" & c.Row).Value * Range("D" & c.Row).Value * 1.05) Else Range("E" & c.Row).Value = "" End If Next End Sub |
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Address() = "$B$3" Then MsgBox "このセルには入力しないで!!" End If End Sub |
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean) If Target.Interior.ColorIndex = xlNone Then Target.Interior.ColorIndex = 3 '赤 ElseIf Target.Interior.ColorIndex = 3 Then Target.Interior.ColorIndex = 6 '黄 ElseIf Target.Interior.ColorIndex = 6 Then Target.Interior.ColorIndex = xlNone End If End Sub |
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim myColor As Variant myColor = Target.Interior.ColorIndex If myColor = xlNone Then myColor = 3 '赤 ElseIf myColor = 3 Then myColor = 6 '黄 ElseIf myColor = 6 Then myColor = xlNone End If Target.Interior.ColorIndex = myColor End Sub |
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean) Dim myColor As Variant myColor = Target.Interior.ColorIndex Select Case myColor Case xlNone myColor = 3 Case 3 myColor = 6 Case 6 myColor = xlNone End Select Target.Interior.ColorIndex = myColor End Sub |
PageViewCounter
Since2006/2/27