A Compile error: Else without If and I don't know why and how to fix

Raceman

Board Regular
Joined
Mar 11, 2010
Messages
64
I'm trying to debug some code and I think the problem area is related to an ElseIf statement. The error message I get is: "Compile error: Else without If" and the row that is highlighed in the Visual Basic viewer is the ElseIf Line. Any help would be appreciated.
'This is option A to add hyperlink automatically. When "Cable Assy..." in column k dropdownn is selected then
HTML:
'a hyperlink is automatically created which links to the next blank cell A on the Cable Matrix Worksheet.
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rws As Long, sh As Worksheet
    Set sh = Worksheets("CABLE_MATRIX")
    With sh
        Rws = .Cells(Rows.Count, "A").End(xlUp).Row + 1
    End With
    If Target.Count = 1 And Target.Column = 11 Then
       If InStr(Target, "Cable Assy") <> 0 Then
           ActiveSheet.Hyperlinks.Add Anchor:=Target.Offset(0, 5), Address:="", SubAddress:= _
                                      "CABLE_MATRIX!A" & Rws, TextToDisplay:="CABLE_MATRIX!A" & Rws
     End If
     
    End If
    
    ' CHECK FOR NEXT CONDITION. Automatic date when an entry is made in Cell A, Q, and S.
   For Each Cell In Target
      If Cell.Column = 1 Then Cell.Offset(0, 11) = Date
      ElseIf Cell.Column = 17 Or Cell.Column = 19 Then
         Cell.Offset(0, 1) = Date
      End If

   Next Cell
End Sub
<!-- BEGIN TEMPLATE: bbcode_html -->
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try

Code:
For Each Cell In Target
      If Cell.Column = 1 Then
        Cell.Offset(0, 11) = Date
      ElseIf Cell.Column = 17 Or Cell.Column = 19 Then
         Cell.Offset(0, 1) = Date
      End If

   Next Cell
 
Upvote 0
This is a Self Contained If (not sure that's the correct term, but it's what I call it)

If Cell.Column = 1 Then Cell.Offset(0, 11) = Date

The 'Action If True' part is written on the same line as the Expression.
So there is no ELSE, and also no END IF

Try changing this part
Code:
      If Cell.Column = 1 Then Cell.Offset(0, 11) = Date
      ElseIf Cell.Column = 17 Or Cell.Column = 19 Then
         Cell.Offset(0, 1) = Date
      End If
to
Code:
    If Cell.Column = 1 Then
        Cell.Offset(0, 11) = Date
    ElseIf Cell.Column = 17 Or Cell.Column = 19 Then
        Cell.Offset(0, 1) = Date
    End If
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top