If cells are empty do not allow save

vimal606

New Member
Joined
Nov 21, 2017
Messages
12
Dear Team,

I have an excel sheet which is filled by line managers. Very often i get back incomplete sheets.

1) I have drop down values enabled in Column "C". If drop down value is selected as "YES", then columns "E" to "H" are mandatory. User should not be able to save the sheet without filling in all the columns. A warning message saying "Please input values in required columns"

2) Colum "D" is also a drop down. If "D" is selected as Yes, there should be a warning message "Please enter training details in Column "G".
The message can be displayed on closing. User can save the excel without data input

3) Maximum allowed "YES" in Column "C" is 10. if the count exceeds 10, a warning message should be displayed as "Exceeds quota"

It would be of great help if you can solve this.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You can do that by counting the number of cells in column C that are equal to "Yes", and then subtracting the number of them column C is "Yes", and all the other cells are populated in columns E-H.
That blankCells calculation would look like:
Code:
    blankCells = Application.WorksheetFunction.CountIf(Range("C:C"), "Yes") - _
        Application.WorksheetFunction.CountIfs(Range("C:C"), "Yes", Range("E:E"), "<>" & "", Range("F:F"), "<>" & "", Range("G:G"), "<>" & "", Range("H:H"), "<>" & "")
 
Last edited:
Upvote 0
Vimal, I have updated the code using Joe's suggestions to correctly determine rows that don't have entries in columns E:H and added the Cancel = True so that the file isn't saved unless all conditions are properly met.
Thanks Joe for your help.

Code:
Option Explicit


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, cancel As Boolean)


Dim i As Long
Dim lr As Long
Dim blankcells As Integer


'   Check to see if there are more than 10 rows in Column C with "Yes"
blankcells = Application.WorksheetFunction.CountIfs(Range("C:C"), "Yes")
If blankcells > 10 Then
    MsgBox "Exceeds Quota"
    cancel = True
End If


'   Check if C = Yes and there are blank cells in Columns E:H
blankcells = Application.WorksheetFunction.CountIf(Range("C:C"), "Yes") - _
        Application.WorksheetFunction.CountIfs(Range("C:C"), "Yes", Range("E:E"), "<>" & "", Range("F:F"), "<>" & "", Range("G:G"), "<>" & "", Range("H:H"), "<>" & "")
If blankcells > 0 Then
    MsgBox "You have entered 'Yes' in Column C" _
    & vbCrLf & "but have left blank cell(s) in " & blankcells & " Rows in  Columns E through H"
    cancel = True
End If


'   Check if D = Yes and there are blank cells in Columns E:H
blankcells = Application.WorksheetFunction.CountIfs(Range("D:D"), "Yes", Range("G:G"), "")
If blankcells > 0 Then
    MsgBox "You have entered 'Yes' in Column D" _
    & vbCrLf & "but have left 'Training Details' blank in  Columns G"
    cancel = True
End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,913
Messages
6,122,207
Members
449,074
Latest member
cancansova

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