PDA

View Full Version : Solved: 'Else without If' error...



AKK
06-23-2009, 11:32 AM
Hi,

I am running the following VBA code on Excel:

Sub FindNewlyClosed()
Dim iRowCount As Integer, iCounter As Integer
Range("H2").Value = ActiveSheet.UsedRange.Rows.Count
Range("B2").Select
If Selection.Value = Selection.Offset(0, -1) Then Range("B3").Select
Else: Range("A2").Select
Selection.Insert Shift:=xlDown
Range("B5").Select
Selection.Font.ColorIndex = 41
Selection.Font.Bold = True
End Sub
When I try to run it, I get an error message that says "Else without If." I don't understand why I'm getting this error since the 'If' is in the line right above it. Can anyone help?

Thanks!

jammer6_9
06-23-2009, 11:38 AM
Give this a try..


Sub FindNewlyClosed()

Dim iRowCount As Integer, iCounter As Integer
Range("H2").Value = ActiveSheet.UsedRange.Rows.Count
Range("B2").Select
If Selection.Value = Selection.Offset(0, -1) Then
Range("B3").Select

Else

Range("A2").Select
Selection.Insert Shift:=xlDown
Range("B5").Select
Selection.Font.ColorIndex = 41
Selection.Font.Bold = True
End If
End Sub

rbrhodes
06-23-2009, 12:26 PM
AKK,

You're missing the closing 'End If'.

Every statement of IF or If/Else or IF/ElseIF/Else, etc needs a closing 'End If' Same with 'WITH' needs an 'End WITH', For/Loop, Do/Loop, For I = #/Next I, Select Case/End Select, etc. I always put the closing in at the same time I enter the opening then build the code in between.

Note: VBA error messages are frequently incorrect. They say 'With' or 'For ' when it's actually and 'IF', things like that! So they can be hard to catch.

jammer6_9
06-23-2009, 12:42 PM
AKK,
Note: VBA error messages are frequently incorrect. They say 'With' or 'For ' when it's actually and 'IF', things like that! So they can be hard to catch.

Exactly :bug: