简体   繁体   English

Excel 2016的强制合并单元格输入

[英]Mandatory Merged Cell Input for excel 2016

I had a merged cell from D4 to H4, I want the cells to be mandatory. 我有一个从D4到H4的合并单元,我希望这些单元是强制性的。 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. 我还需要另外一个单元格H2。 This code already works, I want to combine the codes for D4 to H4 and H2 together. 该代码已经可以使用,我想将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

Any help and suggestion is appreciated. 任何帮助和建议,表示赞赏。

Thanks to @Rik Sportel, I tried to remerged the cells and apply elseif statement and it works now. 感谢@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM