ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   How can I make a cell mandatory? (https://www.excelbanter.com/excel-discussion-misc-queries/69752-how-can-i-make-cell-mandatory.html)

[email protected]

How can I make a cell mandatory?
 
Hello,

I searched the forums and were able to find similar things but cannot
make it work for the action that I need.

I am trying to make B1 a mandatory cell if user puts data in A1. So if
there is data in A1 and B1 is empty, an error message should be
displayed on Save. I am sure it is not very hard but I am not much of
an Excel programmer.

Any help will be greatly appreciated...

Thanks in advance...


[email protected]

How can I make a cell mandatory?
 
Paste the following code into the code section of ThisWorkbook.


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

On Error GoTo ErrorHandler

If Sheets(1).Range("A1") < "" And Sheets(1).Range("B1") = "" Then
Err.Raise vbObjectError + 1, , _
"Value not in B1 when there is a Value in A1"
End If
Exit Sub

ErrorHandler:

Response = MsgBox("Enter a Value into Cell B1", vbOKOnly, "Your
Program Name")

End Sub


[email protected]

How can I make a cell mandatory?
 
I added the Cancel = True to cancel the save in case you wanted to
prevent the save.


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

On Error GoTo ErrorHandler

If Sheets(1).Range("A1") < "" And Sheets(1).Range("B1") = "" Then
Err.Raise vbObjectError + 1, , _
"Value not in B1 when there is a Value in A1"
End If
Exit Sub

ErrorHandler:

Response = MsgBox("Enter a Value into Cell B1", vbOKOnly, "Your
Program Name")
Cancel = True
End Sub


[email protected]

How can I make a cell mandatory?
 
Thanks much for the code but nothing happens when I apply this. Have
you tested it?

emre



All times are GMT +1. The time now is 11:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com