Make cells (in a range) mandatory if another cell(another column range) in row is filled

hussainkottakkal

New Member
Joined
Sep 26, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi there,

Your support is required.

I need an error msg to prompt out and prevent user from saving the file if the particular cell is not fill in.

Meaning if cell in the range C:C is filled up, Range H:H will have to be filled up before the user can save the file.


Thanks!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
@hussainkottakkal, welcome to the Forum.
Let's say the range are "C1:C5" & "H1:H5" in "Sheet1".
Try this:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

With Sheets("Sheet1")
If WorksheetFunction.CountBlank(.Range("C1:C5")) = 0 And WorksheetFunction.CountBlank(.Range("H1:H5")) > 0 Then
    MsgBox "You must ..."
    Cancel = True
End If
End With

End Sub
 
Upvote 0
@hussainkottakkal, welcome to the Forum.
Let's say the range are "C1:C5" & "H1:H5" in "Sheet1".
Try this:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

With Sheets("Sheet1")
If WorksheetFunction.CountBlank(.Range("C1:C5")) = 0 And WorksheetFunction.CountBlank(.Range("H1:H5")) > 0 Then
    MsgBox "You must ..."
    Cancel = True
End If
End With

End Sub
Thanks Akuini, I tried with the script that you provided. But, still this is not giving me the result. Even after entering the given vb i am able to save file without filling column H. Kindly see the attached snap of what I want. Thanks

 

Attachments

  • excel.PNG
    excel.PNG
    17 KB · Views: 20
Upvote 0
My code assumed that "C1:C5" is filled up whille your example shows only "C1:C3" is filled up.
Try this version instead:
Note: you must put the code in ThisWorkbook module.
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim n As Long
With Sheets("Sheet1")
n = .Range("C" & .Rows.Count).End(xlUp).Row
If WorksheetFunction.CountBlank(.Range("C2:C" & n)) = 0 And WorksheetFunction.CountBlank(.Range("H2:H" & n)) > 0 Then
    MsgBox "You must ..."
    Cancel = True
End If
End With

End Sub
 
Upvote 0
Solution
My code assumed that "C1:C5" is filled up whille your example shows only "C1:C3" is filled up.
Try this version instead:
Note: you must put the code in ThisWorkbook module.
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim n As Long
With Sheets("Sheet1")
n = .Range("C" & .Rows.Count).End(xlUp).Row
If WorksheetFunction.CountBlank(.Range("C2:C" & n)) = 0 And WorksheetFunction.CountBlank(.Range("H2:H" & n)) > 0 Then
    MsgBox "You must ..."
    Cancel = True
End If
End With

End Sub
Thank you very much... it worked..!!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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