停止宏运行如果单元格为空并以红色突出显示空单元格

时间:2017-05-16 20:02:01

标签: excel-vba vba excel

我正在尝试运行一个宏来记录放入表单的数据。

运行宏时,它会将信息复制到名为Log sheet的工作表中。然后它会清除表单以供下一个用户提交其详细信息。

我希望只有在Cells (B3,B4,B5,F3,F4,G19,B30,F30)中填写必填字段后才会运行该宏。如果任何单元格为空白,则不要将数据复制到下一页,而是添加Msgbox "Complete the required fields"并突出显示红色的空单元格以完成。

宏用于记录详细信息,但如果未填写上述单元格,我不知道如何阻止它运行。它目前显示Compile Error: Else without If

Sub Submission()

    If Range("B3") = "" Then
      MsgBox "Please insert Outlet Name"
      Range("B3").Select
    End If
    If Range("B4") = "" Then
      MsgBox "Please insert Date"
    End If
    If Range("F3") = "" Then
      MsgBox "Please insert Till Number"
      Range("F3").Select
    End If
    If Range("F4") = "" Then
      MsgBox "Please insert Operator Name"
    End If
    If Range("B5") = "" Then
      MsgBox "Please insert Department Name"
    End If
    If Range("G19") = "" Then
      MsgBox "Please insert Till Reading"
    End If
    If Range("B30") = "" Then
      MsgBox "Please insert Counted By Name"
    End If
    If Range("F30") = "" Then
      MsgBox "Please insert Witnessed By Name"
    Else
      Sheets("Input ").Select
      Sheets("Log Sheet").Visible = True
      Sheets("Log Sheet").Select
      Sheets("Sheet3").Visible = True
      Sheets("Log Sheet").Select
      Rows("2:2").Select
      Selection.Copy
      Application.Goto Cells(Rows.Count, "A").End(xlUp).Offset(1), Scroll:=True
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
      Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
      Sheets("Input ").Select
      Range("B3:C3").Select
      Selection.ClearContents
      Range("F3:H3").Select
      Selection.ClearContents
      Range("F4:H4").Select
      Selection.ClearContents
      Range("B4:C4").Select
      Selection.ClearContents
      Range("B5:C5").Select
      Selection.ClearContents
      Range("C7").Select
      Selection.ClearContents
      Range("C8").Select
      Selection.ClearContents
      Range("C9:C17").Select
      Selection.ClearContents
      Range("G19:H19").Select
      Selection.ClearContents
      Range("C6").Select
      Selection.ClearContents
      Range("A24:H28").Select
      Selection.ClearContents
      Range("C21").Select
      Selection.ClearContents
      ActiveWindow.SmallScroll Down:=9
      Range("B30:C30").Select
      Selection.ClearContents
      Range("F30:H30").Select
      Selection.ClearContents
      Range("C19").Select
      Selection.ClearContents
      Range("C20").Select
      Selection.ClearContents
      Range("B3").Select
      Range("K6:K17").Select
      Selection.ClearContents
      Sheets("Log Sheet").Select
      ActiveWindow.SelectedSheets.Visible = False
      Sheets("Sheet3").Select
      ActiveWindow.SelectedSheets.Visible = False
      Range("B3:C3").Select
      Sheets("Input ").Select
      Range("B3:C3").Select
      ActiveWorkbook.Save
End Sub

1 个答案:

答案 0 :(得分:0)

我已经对你的代码进行了一些调整......还有一些是可能的,但这会让你开始。首先,我已将End If / If替换为ElseIf s。这样,只有在没有触发其他分支时才会触发最终的Else。我还删除了减慢代码速度的所有不必要的select

Sub Submission()

    If Range("B3") = "" Then
      MsgBox "Please insert Outlet Name"
      Range("B3").Select
    ElseIf Range("B4") = "" Then
      MsgBox "Please insert Date"
    ElseIf Range("F3") = "" Then
      MsgBox "Please insert Till Number"
      Range("F3").Select
    ElseIf Range("F4") = "" Then
      MsgBox "Please insert Operator Name"
    ElseIf Range("B5") = "" Then
      MsgBox "Please insert Department Name"
    ElseIf Range("G19") = "" Then
      MsgBox "Please insert Till Reading"
    ElseIf Range("B30") = "" Then
      MsgBox "Please insert Counted By Name"
    ElseIf Range("F30") = "" Then
      MsgBox "Please insert Witnessed By Name"
    Else
      Sheets("Log Sheet").Visible = True
      Sheets("Sheet3").Visible = True
      Sheets("Log Sheet").Rows("2:2").Copy
      With Cells(Sheets("Log Sheet").Rows.Count, "A").End(xlUp).Offset(1)
      .PasteSpecial Paste:=xlPasteValues
      .PasteSpecial Paste:=xlPasteFormats
      End With
      With Sheets("Input ")
      .Range("B3:C5").ClearContents
      .Range("F3:H4").ClearContents
      .Range("C6:C17").ClearContents
      .Range("C19:C20").ClearContents
      .Range("G19:H19").ClearContents
      .Range("A24:H28").ClearContents
      .Range("B30:C30").ClearContents
      .Range("F30:H30").ClearContents
      .Range("K6:K17").ClearContents
      End With
      Sheets("Log Sheet").Visible = False
      Sheets("Sheet3").Visible = False
      ActiveWorkbook.Save
    End If
End Sub