Consulting

Results 1 to 2 of 2

Thread: Help with Compile error: Else with out if

  1. #1

    Help with Compile error: Else with out if

    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
    Last edited by Paul_Hossler; 07-19-2018 at 02:06 PM. Reason: Added CODE tags

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •