Else without if ошибка vba

Здравствуйте. Наверное, напутала что-то с типами данных.. При выполнении данного макроса возникает ошибка «Else without if» :  

  Sub Macros()  
Dim i As Integer  
Dim j As Integer  
Dim x As Integer  
Dim y As Integer  
Dim A As Double  

  x = ActiveCell.Row  
y = ActiveCell.Column  
i = x  

  Do While ((ColorIndex(Cells(i, y))) <> 21)  
   Cells(i, y + 6).Value = Cells(i, y + 1).Value  
   Cells(i, y + 1).Formula = «=MID(RC[-1],SEARCH(«»??.??.????»»,RC[-1],1),10)»
   Cells(i, y + 1).NumberFormat = «m/d/yyyy»  
   Cells(i, y + 2).NumberFormat = «0»  
   Cells(i, y + 3).NumberFormat = «m/d/yyyy»  
   Cells(i, y + 3).Value = Cells(i, y + 1).Value + Cells(i, y + 2).Value  
   Cells(i, y + 4).NumberFormat = «m/d/yyyy»  
   Cells(i, y + 4).Formula = «=TODAY()»  
   Cells(i, y + 5).NumberFormat = «0»  
   Cells(i, y + 6).NumberFormat = «#,##0.00»  
   Cells(i, y + 7).NumberFormat = «#,##0.00»  
   Cells(i, y + 8).NumberFormat = «#,##0.00»  
   Cells(i, y + 9).NumberFormat = «#,##0.00»  
   Cells(i, y + 10).NumberFormat = «#,##0.00»  
   Cells(i, y + 11).NumberFormat = «#,##0.00»  
   Cells(i, y + 12).NumberFormat = «#,##0.00»  
   Cells(i, y + 13).NumberFormat = «#,##0.00»  
   Cells(i, y + 14).NumberFormat = «#,##0.00»  
   Cells(i, y + 5).Value = Cells(i, y + 4).Value — Cells(i, y + 3).Value  
   Cells(i, y + 2).Value = 30  
   A = Cells(i, y + 5).Value  
   If (A <= 0) Then Cells(i, y + 7).Value = Cells(i, y + 6).Value  
   Else: Cells(i, y + 7).Value = Cells(i, y + 16).Value  
   If ((A > 0) And (A <= 30)) Then Cells(i, y + 8).Value = Cells(i, y + 6).Value  
   Else: Cells(i, y + 8).Value = Cells(i, y + 16).Value  
   If ((A > 30) And (A)) Then Cells(i, y + 9).Value = Cells(i, y + 6).Value  
   Else: Cells(i, y + 9).Value = Cells(i, y + 16).Value  
   If ((A > 45) And (A <= 60)) Then Cells(i, y + 10).Value = Cells(i, y + 6).Value  
   Else: Cells(i, y + 10).Value = Cells(i, y + 16).Value  
   If ((A > 60) And (A <= 75)) Then Cells(i, y + 11).Value = Cells(i, y + 6).Value  
   Else: Cells(i, y + 11).Value = Cells(i, y + 16).Value  
   If ((A > 75) And (A <= 90)) Then Cells(i, y + 12).Value = Cells(i, y + 6).Value  
   Else: Cells(i, y + 12).Value = Cells(i, y + 16).Value  
   If (A > 90) Then Cells(i, y + 13).Value = Cells(i, y + 6).Value  
   Else: Cells(i, y + 13).Value = Cells(i, y + 16).Value  
   Cells(i, y + 14).Value = Cells(i, y + 8).Value + Cells(i, y + 9).Value + Cells(i, y + 10).Value + Cells(i, y + 11).Value + Cells(i, y + 12).Value + Cells(i, y + 13).Value  
Loop  
   j = y  
   i = x  
   Do While ((ColorIndex(Cells(i, y))) <> 21)  
       For j = y + 1 To 14  
           Cells(i, j).Interior.ColorIndex = ColorIndex(Cells(i, x))  
           If Cells(i, j).Interior.ColorIndex = 24 Then Cells(i, j).Value = Cells(x, y + 16).Value  
           End If  
       Next j  
   Loop  
   For j = 6 To 14  
       Summa_po_tsvetam (Cells(x, j))  
   Next j  
End Sub  

  Подскажите, пожалуйста, в чем моя ошибка?

I’m trying to run the following code, but I keep getting the Else without If Error in the first sub. The code is supposed to run through a column, open webpages if there is a url in the cell, then save the page info as a text file. If there is no url, then it just saves the text within that file as a text file. I can’t figure out how to change the syntax to get it to work.

Sub LoopOverB()

Dim myRow As Long

myRow = 10

While Worksheets("Input_Format_A").Cells(myRow, 2).value <> ""
    If InStr(1, Worksheets("Input_Format_A").Cells(myRow, 2).value, "http://", vbTextCompare) Then Call url_Test(Worksheets("Input_Format_A").Cells(myRow, 2).value, "C:mallettest" & Worksheets("Input_Format_A").Cells(myRow, 1).value & ".txt")
        myRow = myRow + 1
    Else
        Open "C:mallettest" & Worksheets("Input_Format_A").Cells(myRow, 1) & ".txt" For Append As #1
        Print #1, Worksheets("Input_Format_A").Cells(myRow, 2).value
        Close #1

        myRow = myRow + 1
    End If
Wend
End Sub


Sub url_Test(URL As String, Filename As String)

Dim FSO As Object
Dim ieApp As Object
Dim Txt As String
Dim TxtFile As Object

Set FSO = CreateObject("Scripting.FileSystemObject")
Set TxtFile = FSO.OpenTextFile(Filename, 2, True, -1)

Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Visible = True
ieApp.Navigate URL

While ieApp.Busy Or ieApp.ReadyState <> 4
    DoEvents
Wend

Txt = ieApp.Document.body.innerText
TxtFile.Write Txt
TxtFile.Close

ieApp.Quit

Set ieApp = Nothing
Set FSO = Nothing
End Sub

else without if error popup

What you’re seeing is a compile error that indicates that an Else (or ElseIf) keyword was not preceded by a correct If statement.

Meaning, the compiler found an Else statement (which it will highlight for the user) without seeing an If statement in the lines above it. This is an error because it violates the correct syntax of an If-Then-Else statement. The correct syntax is as follows:

For using the shorthand syntax for if statement that specifies a condition and an action (for when the condition is met):

If [Test Expression] Then [Action]

To use the standard syntax for an If statement that specifies a condition and an action (for when the condition is met) and an alternate action for when the condition is not met:

If [Test Expression] Then

[Action]

Else

[Alternate Action]

End if

For specifying more than one condition (and their actions):

If [Test Expression] Then

[Action]

ElseIf [Test Expression 2] Then

[Action 2]

Else

[Alternate Action]

End if

The compiling error “Else Without If” occurs when “If [Test Expression] Then “ is missing or written incorrectly. Let’s discuss the common causes of the error further in details below with examples.

Missing If Statement

When VBA compiler sees that the Else keyword is not preceded by a recognizable (correct) If statement, it generates an error ‘Else without If’. For every Else, there must be an If statement. However, for every If, we do not necessarily need an else statement.

Example 1: Missing If Statement

In this example, we display an input box that takes the user’s input and stores it in x. Then we encounter an Else keyword, but there was no If statement prior to it. This indeed will cause a compiler error: Else Without If.

Sub Else_Without_If()
x = InputBox("Set x value")'
'if statement should go here, but it’s missing. This will cause a compile error: Else without If
     MsgBox = "x value is equal to 1"
Else
     MsgBox = "x value is not equal to 1"
End If
End Sub

To solve this problem, we just need to add an If statement. An If statement consists of If [condition] Then.

Sub Else_Without_If()
x = InputBox("Set x value")
If x = 1 Then
     MsgBox = "x value is equal to 1"
Else
     MsgBox = "x value is not equal to 1"
End If
End Sub

correct input popup

x value is equal to 1 popup

Following good indentation practices is crucial for recognizing whether each if-else-then logic consists of the necessary  keywords (If statement, Else keyword, End If keyword).

If [condition] Then

[action]

Else

If [condition] Then

[action]

Else

[action]

End If

[action]

End If

Example 2: Else statement is inside a loop and If statement is outside

You might be surprised that you are getting the error when you already have the If statement present in the code and placed prior to the Else keyword and written correctly. The problem might not be apparent at first glance. But if we look closer, we will see that the scope of the If statement is different from that of the else statement.

Everything between the If statement and the End If keyword belongs to the scope of this If statement. Other scopes can also exist in the code due to the existence of loops such as For Next loop, Do Until loop or For Each Next loop.

A scope exists when a logical statement requires multiple keywords to indicate the start and end points of the statement. A problem occurs if you overlap scopes in a way that causes one to be partially placed inside the other. The correct way to have multiple scopes work with each other is to have one totally placed inside the other.

In the following example, If [condition] Then is followed by the beginning of a For loop followed by the Else keyword. This separates the If statement and the Else keyword and causes the error to show up.

Sub Else_Without_If()
x = InputBox("Set x value")
'starting the if statement scope
If x = 1 Then
     MsgBox "x value is equal to 1"
     ‘starting a new scope (For Next loop)
     For R = 1 To 5
'Else keyword is separated from its If statement because it’s in a different scope.
Else
     Next R
     MsgBox "x value is not equal to 1"
End If
End Sub

else without if on example

To fix the issue, we ensure that the If logical statement as a whole is fully encompassed within the For loop. Or the For loop is fully encompassed within the If logical statement Action; between If and Else or between Else and End If.

As a general rule, if a section of your code has a some status due to having a starting and ending point (If – Else, Else – End  If, For – Next, Do – Until) then you cannot start another section (scope) within it without ending it within the first section.

Start Point (If, Else, For, Do)

Another Start Point

Do Something

Another End Point

End Point (Else, End If, Next, Until)

To apply this to our example, we change it to the following code;

Sub Else_Without_If()
x = InputBox("Set x value")
'starting the if statement scope
If x = 1 Then
     MsgBox "x value is equal to 1"
     'starting a new scope (For Next loop)
     For R = 1 To 5
          MsgBox "x value is equal to 1"
     'ending the new scope within the same scope it was started in
     Next R
Else
     MsgBox "x value is not equal to 1"
End If
End Sub

Incorrect If Statement

Example 3: Placing the action on the same line with If Statement

Another very common mistake that is often made is to write the If statement in a way that is not compatible with utilizing the Else keyword.

If we want to use the Else keyword, we must place the action part of the If-Then-Else logic in the next line after the if statement. If we are not using the Else keyword, then we can place the action on the same line as the If statement and we do not write the Else keyword or the End If keyword.

When the action (that should be carried out if the condition is true) is placed on the same line as the If statement, then the If statement is considered complete. This is considered a shorthand way of writing the If – then logic.

That is why when compiler encounters an Else keyword after that, it does not find an If statement that belongs to the Else keyword, because that If statement has been completed with placing the action on the line as the If statement. The following code is NOT correct and will cause the compiler error: Else without If.

Sub Else_Without_If()
x = InputBox("Set x value")
If x = 1 Then MsgBox "x value is equal to 1"
Else
     MsgBox "x value is not equal to 1"
End If
End Sub

The action (MsgBox "x value is equal to 1") needs to be placed on the next row after the If statement in order to be able to have an Else statement used in the If-Then-Else logic. In the following code, we moved in the action statement to the next line, which fixes the problem.

Sub Else_Without_If()
x = InputBox("Set x value")
If x = 1 Then 
     MsgBox "x value is equal to 1"
Else
     MsgBox "x value is not equal to 1"
End If
End Sub

We now have covered all the possible causes of compile error: Else without if. To recap, if you get this error, check whether

1) There is an If statement in place that precedes the Else keyword.

2) The if statement line does not contain anything other than If [condition] Then.

3) Verify that the If statement and the Else keyword are not separated by another scope or section, such as loops.

See also: How to Fix the “End If without block If” Error


Go to excel


VBA: Else without If Error, why am I getting in this error?

So basically I have 3 inputs: Percentage, YrsDeficiency, YrsInsolvency

So I created this formula in VBA, so that I could use it in my model in Excel. Well whenever I try to use it in Excel it puts me back in VBA with the first line of code highlighted saying «Else without If»

How can I fix the code to make it work???

If YrsInsolvency <= 5 Or YrsDeficiency <= 4 Then ZoneColor = "RED"
ElseIf YrsInsolvency <= 7 And Percentage < 0.65 Then ZoneColor = "RED"
ElseIf YrsDeficiency <= 5 And Percentage < 0.65 Then ZoneColor = "RED"

ElseIf Percentage < 0.8 And YrsDeficiency <= 7 Then ZoneColor = "ORANGE"

ElseIf Percentage < 0.8 Or YrsDeficiency <= 7 Then ZoneColor = "YELLOW"

Else: ZoneColor = "GREEN"
End If

This is the whole code (Obviously excluding the first and last line the simply declare the function._

Permalink

Cannot retrieve contributors at this time

title keywords f1_keywords ms.prod ms.assetid ms.date ms.localizationpriority

Else without If

vblr6.chm1011133

vblr6.chm1011133

office

1c95caf3-9b16-bc84-2a6f-da21543895e8

06/08/2017

medium

An Else must be preceded by an If. This error has the following cause and solution:

An Else statement was used without a corresponding If statement. Check other control structures within the If…End If structure and verify that they are correctly matched. Also check that the block If is correctly formatted.

For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).

[!includeSupport and feedback]

Понравилась статья? Поделить с друзьями:
  • Elsawin нелицензированная версия ошибка
  • Emmc id ocr 00ff8080 emmc ошибка включения питания
  • Elsawin error 80004005 неопознанная ошибка
  • Eml ошибка бмв e46
  • Elsa ошибка sql server