PDA

View Full Version : Help with Compile error: Else with out if



Samanthars
07-19-2018, 01:06 PM
Im still learning am are new to trying to write code for a database i need a bit of help as it keep coming up with Compile error: Else with out if



Private Sub AddButton_Click()

If DeptOptionButton5.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Ovine Slaughter").Cells(Sheets("Ovine Slaughter").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If DeptOptionButton6.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Ovine Boning").Cells(Sheets("Ovine Boning").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If DeptOptionButton7.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Stock Yards Beef").Cells(Sheets("Stock Yards Beef").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If DeptOptionButton8.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Bovine Slaughter").Cells(Sheets("Bovine Slaughter").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If DeptOptionButton9.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Bovine Boning").Cells(Sheets("Bovine Boning").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If DeptOptionButton4.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Stock Yard Lamb").Cells(Sheets("Stock Yard Lamb").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If OptionButton10.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Offal").Cells(Sheets("Offal").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If OptionButton6.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Compliance").Cells(Sheets("Compliance").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If OptionButton3.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("CMPI").Cells(Sheets("CMPI").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If OptionButton2.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Salt shed").Cells(Sheets("Salt shed").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If OptionButton4.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Freezers").Cells(Sheets("Freezers").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If OptionButton5.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Trades").Cells(Sheets("Trades").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If OptionButton7.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Admin").Cells(Sheets("Admin").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If OptionButton9.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Managers").Cells(Sheets("Managers").Range("Z1"), 4) = DateTextBox.Value
End If
Else
If OptionButton8.Value = True Then
End If
If CheckBox1.Value = True Then
Sheets("Supervisors").Cells(Sheets("Supervisors").Range("Z1"), 4) = DateTextBox.Value
End If
End If


MsgBox "New Worker training added."
End If
End If


End Sub

Paul_Hossler
07-19-2018, 02:21 PM
1. Welcome to the forums

2. I added CODE and /CODE tags around your macro - you can use the [#] icon to insert them and paste your macro between

3. Worthwhile to read the FAQs (see my sig)

4. I don't think you understand the If-Then-ElseIf- ... -End If structure/syntax

Something like these are not correct or not as you might intend



If DeptOptionButton5.Value = True Then
End If






If CheckBox1.Value = True Then
Sheets("Ovine Slaughter").Cells(Sheets("Ovine Slaughter").Range("Z1"), 4) = DateTextBox.Value
End If
Else





This is example, but you can see how the If (or ElseIf) parts are 'paired' with the End If



If DeptOptionButton5.Value = True Then ' A

If CheckBox1.Value = True Then ' B

Sheets("Ovine Slaughter").Cells(Sheets("Ovine Slaughter").Range("Z1"), 4) = DateTextBox.Value

ElseIf DeptOptionButton6.Value = True Then 'B

If CheckBox1.Value = True Then ' C
Sheets("Ovine Boning").Cells(Sheets("Ovine Boning").Range("Z1"), 4) = DateTextBox.Value
End If ' C
End If 'B

End IF ' A



Since I don't understand your process flow, I can't suggest alternative VBA approaches


I find a macro is more understand able using indents at each level and appropriate white space between blocks

So just re-formatting a piece (PS - you don't really need the '= True' for the Checkboxes) it'd look something like this




Option Explicit

Private Sub AddButton_Click()

If DeptOptionButton5.Value Then

If CheckBox1.Value Then
Sheets("Ovine Slaughter").Cells(Sheets("Ovine Slaughter").Range("Z1"), 4) = DateTextBox.Value

ElseIf DeptOptionButton6.Value Then
If CheckBox1.Value Then
Sheets("Ovine Boning").Cells(Sheets("Ovine Boning").Range("Z1"), 4) = DateTextBox.Value
End If

ElseIf DeptOptionButton7.Value Then
If CheckBox1.Value Then
Sheets("Stock Yards Beef").Cells(Sheets("Stock Yards Beef").Range("Z1"), 4) = DateTextBox.Value
End If

Else

'do something else
End If

MsgBox "New Worker training added."

End Sub



Personal opinion of course -- the computer doesn't care what it looks like