VBA – Events

VBA – Events

Events and Event Procedures were introduced in Excel97. Earlier versions of Excel do not support events.  An Event is an action initiated either by user action or by other VBA code.

 

Workbook Open Event

  1. Open the Visual Basic Editor. (Shortcut- F11)
  2. Double click on This Workbook in the Project Explorer.
  3. Choose Workbook from the left drop-down list. Choose Open from the right drop-down list.
  4. Add the following code line to the Workbook Open Event:

MsgBox “Welcome to VBA World!!!”

  1. Save, close and reopen the Excel file.

 

You will get Message Box Saying “Welcome to VBA World !!! “ at the very beginning once you will open the workbook.

Example to create follow-up worksheet on regular basis

Private Sub Workbook_Open()

Range(“C2”).Activate

Do Until ActiveCell.Value = “”

If (ActiveCell.Value = Date) Or (ActiveCell.Value = Date + 1) Then

ActiveCell.Interior.ColorIndex = 3

ActiveCell.Font.Bold = True

ActiveCell.Offset(0, 1).Value = “Follow Me”

End If

ActiveCell.Offset(1, 0).Activate

Loop

End Sub

 

Example: Worksheet Change Event

Code added to the Worksheet Change Event will be executed by Excel VBA when you change a cell on a worksheet.

 

  1. Open the Visual Basic Editor.
  2. Double click on a sheet (for example Sheet1) in the Project Explorer.
  3. Choose Worksheet from the left drop-down list. Choose Change from the right drop-down list.
  4. The Worksheet Change Event listens to all changes on Sheet1.

 

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Value > 80 Then MsgBox “Goal Completed”

End Sub

 

Example: Worksheet- BeforeDoubleClick Event

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Target.Font.Color = vbRed

End Sub

 

Example: Adding a comment when a cell value is changed

Public preValue As Variant

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub

Target.ClearComments

Target.AddComment.Text Text:=”Previous Value was ” & preValue & Chr(10) & “Edited on ” & Format(Date, “mm-dd-yyyy”) & Chr(10) & “By ” & Environ(“UserName”)

End Sub

 

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub

If Target = “” Then

preValue = “a blank”

Else: preValue = Target.Value

End If

End Sub

 

Example: Create a simple alarm / Event Scheduler program

Sub SetAlarm()

Dim strAlarm As String

strAlarm = InputBox(Prompt:=”At what time? (24-hour clock)”, Title:=”Set Alarm”)

If strAlarm = “” Then Exit Sub

Application.OnTime EarliestTime:=TimeValue(strAlarm), Procedure:=”ShowTime”

End Sub

Private Sub Workbook_Open()

Application.OnKey Key:=”{END}”, Procedure:=”SetAlarm”

End Sub

 

Example: To make a note in the Red if the value of targeted Column is changed (Once the value of any cell from the Column 2 i.e. B is changed)

 

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 2 Then

Cells(Target.Row, 4).Value = “Changed at ” & Date + Time

Range(“d1”).EntireColumn.Select

With Selection

.Locked = True

.FormulaHidden = False

End With

Application.EnableEvents = True

Application.StatusBar = “Value changed”

End If

End Sub

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/08/21/vba-events-2/
YouTube39
YouTube