|
|
|
|
|
|
Excel
|
Force user to enter a value in a cell before leaving the worksheet
|
|
Ease of Use
|
Easy
|
Version tested with
|
2003
|
Submitted by:
|
Ken Puls
|
Description:
|
This code forces a user to enter something in Range("A1") of Sheet1 before they can leave Sheet1. If nothing exists in the cell, the user if returned to the sheet to enter information.
|
Discussion:
|
Sometimes you may want to prohibit a user from leaving a sheet before all required information is filled in. This code will accomplish exactly that.
|
Code:
|
instructions for use
|
Option Explicit
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Dim wsCheck As Worksheet
Dim rngCheck As Range
Set wsCheck = Worksheets("Sheet1")
Set rngCheck = wsCheck.Range("A1")
Application.EnableEvents = False
If Sh.Name = wsCheck.Name Then
If IsEmpty(rngCheck) Then
wsCheck.Activate
rngCheck.Select
MsgBox "Sorry, you must enter a value in " & rngCheck.Address
End If
End If
Application.EnableEvents = True
End Sub
|
How to use:
|
- Copy the code.
- In Excel press Alt + F11 to enter the VBE.
- Press Ctrl + R to show the Project Explorer.
- Expand the desired file on left (in bold).
- In the project explorer, locate the ThisWorkbook object.
- Double click the ThisWorkbook object.
- Paste code into the right pane.
- Change the worksheet name or range name (not the wsCheck portion) to desired values.
- Press Alt + Q to close the VBE.
- Save workbook before any other changes.
|
Test the code:
|
- Select another sheet in the workbook.
- When you receive the error message and are returned to sheet 1, enter something in cell A1 (if using the defaults).
- You should now be able to select another sheet in the workbook without a message.
|
Sample File:
|
ForceData.zip 7.89KB
|
Approved by mdmackillop
|
This entry has been viewed 179 times.
|
|