[英]Stop Macro Running If Cell is empty and highlight empty cells in red
I am trying to run a macro that records data put into a form. 我正在尝试运行一个宏,该宏记录放入表单的数据。
When the macro is run, it copies the information to a worksheet called Log sheet
. 运行宏时,它将信息复制到一个名为
Log sheet
的工作Log sheet
。 It then clears the form for the next user to submit their details. 然后,它清除表单,让下一个用户提交其详细信息。
I would like the macro to only run once the required fields have been filled in Cells
(B3,B4,B5,F3,F4,G19,B30,F30)
. 我希望宏仅在
Cells
(B3,B4,B5,F3,F4,G19,B30,F30)
填充所需字段后才运行。 If any of the cells are blank, then do not copy the data to the next page but add Msgbox "Complete the required fields"
and highlight the empty cells in red to be completed. 如果任何单元格为空,则不要将数据复制到下一页,而是添加
Msgbox "Complete the required fields"
并用红色突出显示要完成的空单元格。
The macro works for recording details, but I do not know how to prevent it from running if the above cells are not filled in. It currently shows Compile Error: Else without If
. 该宏用于记录详细信息,但是如果不填写上述单元格,我不知道如何防止其运行。当前显示的是“
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
I've made some tweaks to your code ... several more are possible, but this will get you started. 我对您的代码进行了一些调整...可能还会有更多调整,但这将使您入门。 Firstly, I've replaced your
End If / If
s with ElseIf
s. 首先,我用
ElseIf
替换了End If / If
。 This allows the final Else
to be triggered only if no other branch is triggered. 这仅在没有其他分支被触发时才触发最终
Else
。 I've also removed all the unncessary select
s which slow down the code. 我还删除了所有不必要的
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.