VBA:否则没有如果错误

问题描述:

我想运行下面的代码,但我不断收到没有如果错误在第一个子。代码应该在列中运行,如果单元格中有URL,则打开网页,然后将页面信息保存为文本文件。如果没有url,那么它只是将该文件中的文本保存为文本文件。我无法弄清楚如何改变语法来使它工作。VBA:否则没有如果错误

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 
+0

你可以在你的代码中修复回车吗?很难判断遗漏回车是否是问题,或者是由于您粘贴代码的方式引起的。 – Keith 2013-03-06 15:55:26

+2

我可以建议您安装并使用智能缩进(http://www.oaltd.co.uk/Indenter/)还是努力让正确缩进而不是混乱? – 2013-03-06 16:04:08

与第一If行了,你必须去到一个新行Then后,否则你If将隐含关闭。

'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 
+0

感谢您的帮助! – Momo 2013-03-06 16:07:10

+2

聪明的压头立即强调了这个问题,因为'Else'没有与'If' – SeanC 2013-03-06 18:48:46

+0

+1 *排列在一起,我需要在* QBASIC *上完成我的兄弟功课,并且正在为我的头脑开裂,为什么它不工作。其有趣的是,这虽然有效 – 2014-07-22 15:27:32