Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compile Error "Else without If"

Status
Not open for further replies.

dipitiduda2

Technical User
Mar 17, 2003
18
US
Hello,

I need assistance with a compile error that I cannot resolve - "Else without If". The error occurs when the it hits the first 'Else' within "Budget Form (2)" code (see code below).

Here's what I'm trying to do: Copy Budget Form to Budget Form 2 if user clicks on cmdBtn, else return to Budget Form. Deactivate the cmdBtn on Budget Form after creating Budget Form 2. Repeat scenario if Budget Form 3 is created, ......and so on.

Any help is appreciated. Thanks!

Private Sub Adjustment_Add_Btn_Click()
ActiveSheet.Select
ActiveSheet.Unprotect
ActiveSheet.Adjustment_Add_Btn.Enabled = False
ActiveSheet.Copy after:=ActiveSheet
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True


If Sheets("Budget Form (2)").Active Then Sheets("Budget Form (2)").Activate
With Sheets("Budget Form (2)")
.Select
.Unprotect
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
Else
Sheets("Budget Form").Activate
End If
Else
If Sheets("Budget Form (3)").Active Then Sheets("Budget Form (3)").Activate
With Sheets("Budget Form (3)")
.Select
.Unprotect
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
Else
Sheets("Budget Form (2)").Activate
End If
Else
If Sheets("Budget Form (4)").Active Then Sheets("Budget Form (4)").Activate
With Sheets("Budget Form (4)")
.Select
.Unprotect
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
Else
Sheets("Budget Form (3)").Activate
End If
Else
If Sheets("Budget Form (5)").Active Then Sheets("Budget Form (5)").Activate
With Sheets("Budget Form (5)")
.Select
.Unprotect
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
Else
Sheets("Budget Form (4)").Activate
End If
End If

End Sub
 
Your nesting is confusing the heck out of me...
Is this what you're trying to do? If so, why does an "ELSE" follow your end if? Where does it belong?


If Sheets("Budget Form (2)").Active Then
Sheets("Budget Form (2)").Activate
With Sheets("Budget Form (2)")
.Select
.Unprotect
Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
Else
Sheets("Budget Form").Activate
End If Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
 
Try to indent your "If" statements in pairs with you "End If" statements like this

If This Then
' code here
If That Then
'more code
Else
If Something Then
'code again
End If
End If
Else
'other code
End If

You should be able to pick out your missing statements.

Hope this helps.
If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Look up VBHelp on basic If...Then structure

You can only use [tt]Else[/tt] and [tt]End If[/tt] after you have used an [tt]If <condition> Then[/tt] statement on a line on its own
[tt]
If a Then
'Do this
Else
'Do that
End If
[/tt]
You have used the basic
[tt]If a Then Do this[/tt]

using this structure completes the If section - you can't continue with Else or End If
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
My responses: I appreciate all of your assistance.....I'm still struggling with the logic of this procedure....new at this stuff.
To: johnwm, I did consult the VBA help feature.
To: mwolf00, I need to work more on the nesting to get it right.
To: foada, I'm using your logic and should be able to fix the statements with it.
 
i think what johnwm is saying is your structure isnt correct

if <condition> then
'do this
elseif <condition> then
'do this
elseif <another condition> then
'do this
else
'do the last possible thing
end if

your code is:-

If Sheets(&quot;Budget Form (2)&quot;).Active Then

With Sheets(&quot;Budget Form (2)&quot;)
.Activate
.Select
.Unprotect
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Else
Sheets(&quot;Budget Form&quot;).Activate
End If


hope this sets you straight!!
If somethings hard to do, its not worth doing - Homer Simpson
 
I will try to clarify

In the first post in this thread the code posted was:
If Sheets(&quot;Budget Form (2)&quot;).Active Then Sheets(&quot;Budget Form (2)&quot;).Activate

What I am saying is that if you use this structure with a statement on the end of the 'If' line, you can't use Else on a separate line
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
But you can use and ELSE statement on the same line

IF condition THEN expression1 ELSE expression2

Is the same as

IF condtion THEN
expression1
ELSE
expression2
END IF


But you cannot say

IF condition THEN expression1
ELSE confition2


Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top