Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA: Else without If Error

Tags:

excel

vba

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:\mallet\test\" & Worksheets("Input_Format_A").Cells(myRow, 1).value & ".txt")
        myRow = myRow + 1
    Else
        Open "C:\mallet\test\" & 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
like image 846
Momo Avatar asked Mar 06 '13 15:03

Momo


People also ask

Why am I getting an else without if error in VBA?

What you're seeing is a compile error that indicates that an Else (or ElseIf) keyword was not preceded by a correct If statement. The compiling error “Else Without If” occurs when “ If [Test Expression] Then “ is missing or written incorrectly.

How do I fix error else without if?

Solution: The above compilation error can be resolved by providing correct if-elseif-else syntax as shown below. The best way to deal with error: 'else' without 'if' is always to use curly braces to show what is executed after if. Learn to indent the code too as it helps in reading and understanding.

What is the meaning of else without if error?

'else' without 'if' This error means that Java is unable to find an if statement associated to your else statement. Else statements do not work unless they are associated with an if statement. Ensure that you have an if statement and that your else statement isn't nested within your if statement.

Is there an else if in VBA?

There can be multiple Else If…Then clauses in a VBA if statement, as long as each Else If … criteria is mutually exclusive from other Else If or If criteria. End If should close the last Else If…Then statement. In the above example, if Cell A5 is 91, then Cell A6 will be set to Decent Performance.


1 Answers

On the line with the first If, you must go to a new line after Then, otherwise your If will be implicitely closed.

'Good (in this case)
If <condition> Then    
   DoSomething
   myRow = myRow + 1
Else
   DoSomething Different
End if


'NOT good 
If <condition> Then  DoSomething  
   'the if is now "closed" !!!
   myRow = myRow + 1
Else
   DoSomething Different
End if
like image 134
iDevlop Avatar answered Nov 15 '22 08:11

iDevlop