compile error: Help Please

eltonstaka

New Member
Joined
Jun 20, 2013
Messages
2
Block if without end if

Private Sub Worksheet_Change(ByVal Target As Range)
' MsgBox (Target.Rows)
If Target.Column = 9 Or Target.Column = 10 Then
Dim v1 As Single
Dim vx As Single
v1 = Cells(Target.Row, 9)
vx = Cells(Target.Row, 10)
If v1 > 1 And vx > 1 Then
Dim ac As Worksheet
If Cells(Target.Row, 8) = 1 Then
Set ac = Worksheets("Kuota RF_1_92")
Else
If Cells(Target.Row, 8) = 1 Then
Set ac = Worksheets("Kuota RF_1_90")
Else
If Cells(Target.Row, 8) = 3 Then
Set ac = Worksheets("Kuota RF_1_88")
End If

For i = 2 To 1000
If ac.Cells(i, 1) = v1 And ac.Cells(i, 2) = vx Then
Cells(Target.Row, 11) = ac.Cells(i, 3)
Exit For
End If

Next
End If


End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Each IF needs an End If (unless the 'action if true' is written on the same line as the if expression)
So you have 6 If's that need an End If, but you only have 2 End Ifs

You're putting new IF's in the Else part of a previous IF.
That If in the Else part is it's own IF, and needs an End if too

This compiles and runs without error, but I have no idea if it actually does what you want..

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' MsgBox (Target.Rows)
If Target.Column = 9 Or Target.Column = 10 Then
    Dim v1 As Single
    Dim vx As Single
    v1 = Cells(Target.Row, 9)
    vx = Cells(Target.Row, 10)
    If v1 > 1 And vx > 1 Then
        Dim ac As Worksheet
    End If
    If Cells(Target.Row, 8) = 1 Then
        Set ac = Worksheets("Kuota RF_1_92")
    Else
        If Cells(Target.Row, 8) = 1 Then
            Set ac = Worksheets("Kuota RF_1_90")
        Else
            If Cells(Target.Row, 8) = 3 Then
                Set ac = Worksheets("Kuota RF_1_88")
            End If
        End If
    End If
    
    For i = 2 To 1000
    If ac.Cells(i, 1) = v1 And ac.Cells(i, 2) = vx Then
        Cells(Target.Row, 11) = ac.Cells(i, 3)
        Exit For
    End If
    
    Next
End If

End Sub
 
Upvote 0
That is a hell of a nested if...but this compiled correctly for me. I've found tabbing on new lines within a statement helps to find where to close it.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim v1 As Single, vx As Single
Dim ac As Worksheet
Dim i As Integer

    If Target.Column = 9 Or Target.Column = 10 Then
        v1 = Cells(Target.Row, 9)
        vx = Cells(Target.Row, 10)

        If v1 > 1 And vx > 1 Then
            If Cells(Target.Row, 8) = 1 Then
                Set ac = Worksheets("Kuota RF_1_92")
                Else
                    If Cells(Target.Row, 8) = 1 Then
                        Set ac = Worksheets("Kuota RF_1_90")
                        Else
                            If Cells(Target.Row, 8) = 3 Then
                                Set ac = Worksheets("Kuota RF_1_88")
                            End If
                    End If
            End If
        End If

        For i = 2 To 1000
            If ac.Cells(i, 1) = v1 And ac.Cells(i, 2) = vx Then
                Cells(Target.Row, 11) = ac.Cells(i, 3)
                Exit For
            End If
        Next
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,713
Members
449,464
Latest member
againofsoul

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