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
. 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)
. 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.
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
.
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