[英]Mandatory Merged Cell Input for excel 2016
我有一个从D4到H4的合并单元,我希望这些单元是强制性的。 我尝试了以下代码,但无法正常工作。
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
我还需要另外一个单元格H2。 该代码已经可以使用,我想将D4到H4和H2的代码组合在一起。
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
任何帮助和建议,表示赞赏。
感谢@Rik Sportel,我尝试重新合并单元格并应用elseif语句,现在它可以工作了。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.