I had a merged cell from D4 to H4, I want the cells to be mandatory. I had tried the following code but does not work.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim c As Range
Dim cel As Range
Set c = Range("D4:H4")
Application.EnableEvents = False
If Not Intersect(Target, c) Is Nothing Then
For Each cel In c
If cel.Text = "" Then
Application.Goto reference:=c, Scroll:=True
MsgBox ("Field Cannot Be Blank")
Exit For
End If
Next cel
End If
Application.EnableEvents = True
End Sub
I also had another cell need to be mandatory, H2. This code already works, I want to combine the codes for D4 to H4 and H2 together.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Cells(2, 8).Value = "" Then
MsgBox "Cell H2 requires user input", vbInformation, "Please filled up the
mandatory cells"
Cancel = True
End If
End Sub
Any help and suggestion is appreciated.
Thanks to @Rik Sportel, I tried to remerged the cells and apply elseif statement and it works now.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Cells(4, 4).Value = "" Then
MsgBox "Cell D4 requires user input", vbInformation, "Please filled up the mandatory cells"
Cancel = True
ElseIf Cells(2, 8).Value = "" Then
MsgBox "Cell H2 requires user input", vbInformation, "Please filled up the mandatory cells"
Cancel = True
End If
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.