簡體   English   中英

Excel 2016的強制合並單元格輸入

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM