Riley has a worksheet that he needs to work with every Friday. This worksheet is based on a template, and there are, at minimum, five cells he needs to fill in, in the range C4:C8. When he starts with the worksheet, these cells are blank. Riley wonders if there is a way to prevent the worksheet from being saved and/or closed until he fills in all five of these cells.
There is a way to do this, but it involves the use of macros. Excel supports the concept of event handlers, which means that you can develop macros that run, automatically, when certain events occur. Two events for which you can create special event handlers are BeforeClose (meaning, before the workbook closes) and BeforeSave (before the workbook is saved).
As an example of how this could work, let's say that the worksheet containing the range to be checked (C4:C8) is named "MyData." You could add this code to the ThisWorkbook module:
Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim rng As Range Dim iCount As Integer Dim sTemp As String Set rng = Worksheets("MyData").Range("C4:C8") iCount = Application.WorksheetFunction.CountBlank(rng) If iCount <> 0 Then sTemp = rng.Address & " has blank cells. " & vbCrLf sTemp = sTemp & "The workbook will not be closed." MsgBox sTemp Cancel = True End If End Sub
Note that the macro relies on the CountBlank worksheet function to determine if there are any blanks in the range of cells. If it detects any blank cekks (iCount is greater than 0), then the macro displays a message to the user and the Cancel variable is set to True, which actually stops the workbook from closing.
You can use a similar macro for the BeforeSave event, in this manner:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim rng As Range Dim iCount As Integer Dim sTemp As String Set rng = Worksheets("MyData").Range("C4:C8") iCount = Application.WorksheetFunction.CountBlank(rng) If iCount <> 0 Then sTemp = rng.Address & " has blank cells. " & vbCrLf sTemp = sTemp & "The workbook will not be saved." MsgBox sTemp Cancel = True End If End Sub
If you wanted to make sure that the routine selected the cells in which input is neeed (as a final step), you could add the following line to both macros, right after the line that sets the Cancel variable to True:
rng.Select
Remember, as well, that since your workbook is based on a template, it will need to be saved as a macro-enabled template in order to work properly.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (4364) applies to Microsoft Excel 2007, 2010, 2013, and 2016.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
The VBA programming language provided with Excel allows you to create and modify text files quite easily. Here's how to ...
Discover MoreOne of the things you can do with macros is to work with disk files. As you do so, you may have a need to create a new ...
Discover MoreWhen creating macros, you often have to know how to display individual worksheets. VBA provides several ways you can ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2017-10-09 07:26:58
Nick from London
I saw the query some time after it was posed.
if you are going to use a macro, you could consider a User Form that opens when the Template opens and asks for the items of information, possibly with a user friendly description. The form could be designed only to close once the data had been input.
2017-10-09 04:06:44
Lionel ACHARD
rng.Select doesn't select the empty cells, you could rather use rng.SpecialCells(xlCellTypeBlanks).Select.
If the form is long, the missing values could be hiden on the bottom of the page, or event on an other sheet.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments