473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Compiler Error: Else without If

3
Hi, I'm trying to code a button to hide and show data in cells H5:I9 (MS Excel 2002)

Here's the code:

Expand|Select|Wrap|Line Numbers
  1. Sub Button21_Click()
  2. If Range("H5:H9").NumberFormat = "General" And Range("I5:I9").NumberFormat = "0.00%" Then Range("H5:I9").SelectSelection.NumberFormat = ";;;"
  3. Else: If Range("H5:I9").NumberFormat = ";;;" Then Range("H5:I9").Select
  4.     Selection.NumberFormat = "General"
  5.     Range("I5:I9").Select
  6.     Selection.NumberFormat = "0.00%"
  7.  
  8. End Sub
It appears to be ok (messy I know, but valid) yet I get a compiler error saying "Else without If".
What's wrong? It's driving me nuts!
Nov 21 '07 #1
7 5041
daniel aristidou
491 256MB
Hi, I'm trying to code a button to hide and show data in cells H5:I9 (MS Excel 2002)

Here's the code:

Sub Button21_Click()
If Range("H5:H9").NumberFormat = "General" And Range("I5:I9").NumberFormat = "0.00%" Then Range("H5:I9").SelectSelection.NumberFormat = ";;;"
Else: If Range("H5:I9").NumberFormat = ";;;" Then Range("H5:I9").Select
Selection.NumberFormat = "General"
Range("I5:I9").Select
Selection.NumberFormat = "0.00%"

End Sub

It appears to be ok (messy I know, but valid) yet I get a compiler error saying "Else without If".
What's wrong? It's driving me nuts!
I believe you missed out 2 end ifs

TRy adding
End if
End if

Before end sub
ie like this;
Expand|Select|Wrap|Line Numbers
  1. Sub Button21_Click()
  2. If Range("H5:H9").NumberFormat = "General" And Range("I5:I9").NumberFormat = "0.00%" Then Range("H5:I9").SelectSelection.NumberFormat = ";;;"
  3. Else: If Range("H5:I9").NumberFormat = ";;;" Then Range("H5:I9").Select
  4.     Selection.NumberFormat = "General"
  5.     Range("I5:I9").Select
  6.     Selection.NumberFormat = "0.00%"
  7. End if
  8. End if
  9. End Sub
Nov 21 '07 #2
daniel aristidou
491 256MB
All if statements have to be ended with end if. In your case i belive you had 2 if statments without end ifs
Nov 21 '07 #3
Stu120
3
That still doesn't work.
If it helps I'm trying to link two macros into one, which were recorded as:
1: To show data -

Expand|Select|Wrap|Line Numbers
  1. Sub Show_Calculations()
  2.  
  3.     Range("H5:I10").Select
  4.     Selection.NumberFormat = "General"
  5.     Range("I5:I9").Select
  6.     Selection.NumberFormat = "0.00%"
  7. End Sub
and 2: To hide data -

Expand|Select|Wrap|Line Numbers
  1. Sub Hide_Calculations()
  2.  
  3.  Range("H5:I10").Select
  4.     Selection.NumberFormat = "General"
  5.     Range("I5:I9").Select
  6.     Selection.NumberFormat = "0.00%"
  7.     Range("H5:I9").Select
  8.     Selection.NumberFormat = ";;;"
  9. End Sub
Nov 21 '07 #4
Ali Rizwan
925 512MB
Hi, I'm trying to code a button to hide and show data in cells H5:I9 (MS Excel 2002)

Here's the code:

Expand|Select|Wrap|Line Numbers
  1. Sub Button21_Click()
  2. If Range("H5:H9").NumberFormat = "General" And Range("I5:I9").NumberFormat = "0.00%" Then Range("H5:I9").SelectSelection.NumberFormat = ";;;"
  3. Else: If Range("H5:I9").NumberFormat = ";;;" Then Range("H5:I9").Select
  4.     Selection.NumberFormat = "General"
  5.     Range("I5:I9").Select
  6.     Selection.NumberFormat = "0.00%"
  7.  
  8. End Sub
It appears to be ok (messy I know, but valid) yet I get a compiler error saying "Else without If".
What's wrong? It's driving me nuts!

Try this code i have made some modifications to it.

Expand|Select|Wrap|Line Numbers
  1. Sub Button21_Click()
  2.  
  3. If Range("H5:H9").NumberFormat = "General" And Range("I5:I9").NumberFormat = "0.00%" Then 
  4. Range("H5:I9").SelectSelection.NumberFormat = ";;;"
  5. Else
  6.       If Range("H5:I9").NumberFormat = ";;;" Then Range("H5:I9").Select
  7. Selection.NumberFormat = "General"
  8. Range("I5:I9").Select
  9. Selection.NumberFormat = "0.00%"
  10.  
  11. End If
  12.  
  13. End Sub
Nov 25 '07 #5
Killer42
8,435 Expert 8TB
Stu120, I think what you need to understand here is that VB supports two different structures for the IF statement. One is single-line, the other multi-line.

The Single-line structure is...
IF <test> Then <statement> [Else <statement>]
The multi-line structure is...
If <test> Then
<statement> ...
[Else
<statement>...]
End If
A multi-line If statement must have an End If. A single line If statement cannot have an End If (or a separate Else clause/statement).

Because you originally wrote the Then on the same line as the If, you made it the single-line version. Thus, the Else which was encountered later didn't belong anywhere.
Nov 26 '07 #6
Killer42
8,435 Expert 8TB
Try this code ...
I think you're making things rather confusing by jumbling together the two different IF structures. Try this version...
Expand|Select|Wrap|Line Numbers
  1. Sub Button21_Click()
  2.  
  3.   If Range("H5:H9").NumberFormat = "General" And Range("I5:I9").NumberFormat = "0.00%" Then 
  4.     Range("H5:I9").SelectSelection.NumberFormat = ";;;"
  5.   ElseIf Range("H5:I9").NumberFormat = ";;;" Then
  6.     Range("H5:I9").Select
  7.     Selection.NumberFormat = "General"
  8.     Range("I5:I9").Select
  9.     Selection.NumberFormat = "0.00%"
  10.   End If
  11.  
  12. End Sub
Nov 26 '07 #7
Stu120
3
Ok that works. Thanks.
Dec 1 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: paul calvert | last post by:
I hope somewhere here has encountered and solved a similar problem in the past. 1) on a new Win2000 PC: installed Visual C++ 6.0 download & install single file Service Pack 5.0 2) try to...
6
by: rbazinet | last post by:
I have a VS 2003 C# project, web app with a bunch of DLL's. When I compile my project I often times get this message: Unexpected error creating debug information file 'C:\DevProjects\Allstar...
7
by: Matthew Del Buono | last post by:
Don't try to solve the problem. I've found a way -- around or fixing it. I'm just curious as to whether this is Microsoft's problem in their compiler or if there's a standard saying this is to be...
3
by: Benedikt Weber | last post by:
I found the following compiler error with Microsoft Visual C++ .NET. I use different functions with return types determined by a Traits class. Function g() below works ok, but when I put the two...
9
by: JTrigger | last post by:
When I compile my project using the IDE on a development machine it works just fine. When I compile it on the server using csc.exe, I get the following error when I try to bring it up in the web...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
8
by: STG | last post by:
Greetings, My group has an SDK that was developed 5 years ago with VC++ 6. Over the last years, the requests for a VS.NET SDK has reached critical mass and I am now in the process of doing that....
3
by: dancer | last post by:
I am using Framework 1.1.4322. Who can tell me why I'm getting this error? My code follows Compilation Error Description: An error occurred during the compilation of a resource required to...
13
by: Albert | last post by:
Hi I'm using the lcc compiler for win32. I tried compiling a program but there's an error stating: "cpp: Can't open input file clrscr()" I don't get it - I've included <tcconio.h>. (strange why...
27
by: Dave | last post by:
I'm having a hard time tying to build gcc 4.3.1 on Solaris using the GNU compilers. I then decided to try to use Sun's compiler. The Sun Studio 12 compiler reports the following code, which is in...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.