PDA

View Full Version : Solved: Force user to fill cell



sujittalukde
06-06-2007, 05:13 AM
In the attached file if the user does not filled in some data, a message box should appear before closing the wb, and force the user to fill those data before closing the workbook so that even by mistake user does not forget to fill those cells.

Bob Phillips
06-06-2007, 05:34 AM
Private Sub Workbook_BeforeClose(Cancel As Boolean)
With Worksheets("Sheet1")
If .Range("D2").Value = "" Then
MsgBox "Month must be supplied"
Cancel = True
ElseIf .Range("E2").Value = "" Then
MsgBox "Year must be supplied"
Cancel = True
End If
End With
End Sub


This is workbook event code.
To input this code, right click on the Excel icon on the worksheet
(or next to the File menu if you maximise your workbooks),
select View Code from the menu, and paste the code

sujittalukde
06-06-2007, 05:44 AM
But it does not cover the cells related to number entries ie green coloured cells!

lucas
06-06-2007, 07:19 AM
Bob showed you how to do it and got you started. You have to do the busy work yourself...I did the next one for you cell B5:


Private Sub Workbook_BeforeClose(Cancel As Boolean)
With Worksheets("Sheet1")
If .Range("D2").Value = "" Then
MsgBox "Month must be supplied"
Cancel = True
ElseIf .Range("E2").Value = "" Then
MsgBox "Year must be supplied"
Cancel = True
ElseIf .Range("B5").Value = "" Then
MsgBox "Cell B5 Must be filled in"
Cancel = True
Range("B5").Select
End If
End With
End Sub

sujittalukde
06-06-2007, 10:05 PM
Thanks lucas, rest I can do One thing , Can I set a range in macro in place of cell B5,B6,etc. something like B5:E7,B9:E12 If yes, just supply the lines i will modify the same in the code

Bob Phillips
06-07-2007, 01:56 AM
ElseIf Application.Counta(.Range("B5:E7,B9:E12")) = 0 Then

sujittalukde
06-07-2007, 02:23 AM
thanks xld!

geekgirlau
06-07-2007, 10:21 PM
Actually, in order to make sure all of those cells are filled in I think you'll need:


ElseIf Application.Counta(.Range("B5:E7,B9:E12")) <> 28 Then

sujittalukde
06-07-2007, 10:24 PM
Thanks geek !

Bob Phillips
06-08-2007, 01:45 AM
or to not be range size bound



ElseIf Application.CountBlank(.Range("B5:E7,B9:E12")) = 0 Then

sujittalukde
06-08-2007, 01:48 AM
Again, Thanks to you, xld !

Bob Phillips
06-08-2007, 01:50 AM
Okay, sorry I got it wrong first time.