Prevent user from closing excel unless two cells are equal

Niven

New Member
Joined
Aug 23, 2011
Messages
45
Hi,

I am creating a timesheet for users to enter their daily working hours as well as an allocation of their total hours worked. The allocation is very important as they work in different projects.

Is there a way I can prevent the workbook from closing and an error message appearing to let them know that their total hours need to equal the hours allocated?

Thanks.
Niven
 
Sorry I tried this but I get an error message and then "blnCancel = Not Sheets("Sheet1").Range("A1").Value" is highlighted in yellow.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
The name is Sheet 1 but the yellow highlight still appears. I changed the cell so I thought that was the problem but now I have the correct cell.
 
Upvote 0
What's the cell address?
What is displayed in the cell address?
What is the exact code you are using?
What is the error description that shows up?
 
Upvote 0
The cell address is C44 and in that cell I have =AND(H24:H31=K24:K31). I did do the Ctrl+Shift+Enter and it came up with the two curly brackets.

The code I am using is:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim blnCancel As Boolean
Dim rngCell1 As Excel.Range, rngCell2 As Excel.Range
blnCancel = Not Sheets("Sheet1").Range("C44").Value
If blnCancel Then
Call MsgBox(Prompt:="Have ALL hours worked been allocated to a project?", Title:="Cannot close workbook", Buttons:=vbExclamation + vbOKOnly)
End If
Cancel = blnCancel
End Sub

The error message I get is Run-time error '9' Subscript out of range
 
Upvote 0
Either True or False depending on whether the cells match. I tried it and it changes to True or False
 
Upvote 0
And you say it breaks here?
Rich (BB code):
blnCancel = Not Sheets("Sheet1").Range("C44").Value

That doesn't make sense to me. You say your sheet is called "Sheet1" and that C44 contains either TRUE or FALSE. This statement should always only return the inverse of what is in C44.

Sorry; I'm stumped.
 
Upvote 0
The error you get is consistent with a misspelled sheet name. I don't suppose you have a space in the name i.e. "Sheet 1"?
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,616
Members
449,238
Latest member
wcbyers

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top