Mandatory Cells before leaving sheet

Leis_B

New Member
Joined
Oct 5, 2017
Messages
7
Hi! Apologies if this has been posted elsewhere, I just can't seem to find anything that suits my purpose.

What I need to do, is check that a range of cells have a value entered before they can move off the sheet, using the button (I realise they can just move between sheets using the tabs). I've come up with this and it works, but when testing it and going back and removing the values, it won't activate the msgbox. What am I doing wrong?

Sub Mandatory1()
With Sheets("FORM_SCHEDNEWHIRE")
If IsEmpty(.Range("F5,F7,F9,F11,F13,F15,F17,F19,F21,F23,F25,F27,F31,F33,F35,F37")) Then
MsgBox "Please ensure you have answered all of the questions before moving on.", vbCritical
Application.Goto .Range("F5,F7,F9,F11,F13,F15,F17,F19,F21,F23,F25,F27,F31,F33,F35,F37")
Else
Sheets("FORM_BANK_SUPER").Select
Range("F6:I6").Activate
End If
End With
End Sub

Thanks in advance.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Why not use an event macro to make things automatic? Here's one you can try. This code goes in the sheet - see instructions below the code for installing it.
Code:
Private Sub Worksheet_Deactivate()
Dim c As Range, Unfilled As String
For Each c In Range("F5,F7,F9,F11,F13,F15,F17,F19,F21,F23,F25,F27,F31,F33,F35,F37")
    If IsEmpty(c) Then Unfilled = Unfilled & ", " & c.Address(0, 0)
Next c
If Unfilled <> "" Then
    MsgBox "Please answer the questions related to cells: " & Unfilled & " before leaving this sheet"
    Me.Select
Else
    Sheets("FORM_BANK_SUPER").Select
    Activesheet.Range("F6:I6").Activate
End If
End Sub
To install sheet code:
1. Right-click the worksheet tab you want to apply it to and choose 'View Code'. This will open the VBE window.
2. Copy the code below from your browser window and paste it into the white space in the VBE window.
3. Close the VBE window and Save the workbook. If you are using Excel 2007 or a later version do a SaveAs and save it as a macro-enabled workbook (.xlsm file extension).
4. Make sure you have enabled macros whenever you open the file or the code will not run.
 
Last edited:
Upvote 0
You are welcome - thanks for the reply.

Hi JoeMo,

Thanks again for your help with this, I was hoping to impose on you a little further.

The coding does exactly what I want it to, however I need to send the sheet out empty in the first place, but I am now on this perpetual loop. Is there a way that I can save it "empty", so it is ok to send out to users? Was thinking that it could look at a different cell that I could have hidden or something that would "trigger" the mandatory cell coding? I just have no idea where to start on that...

Thanks in advance.
 
Upvote 0
Clear whatever contents you want on the sheet. Then, without leaving the sheet, save the workbook.
 
Upvote 0
Thanks JoeMo! Unfortunately, I have multiple sheets in my workbook that I have replicated the code with.

Any ideas?
 
Upvote 0
Give me the sheet names and the range for each sheet you want to clear when the user opens the workbook.
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,891
Members
449,194
Latest member
JayEggleton

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