VBA:Else没有If Error

Mom*_*omo 6 excel vba excel-vba

我正在尝试运行以下代码,但是我继续在第一个子中没有If Error的情况下获得Else.代码应该通过列运行,如果单元格中有url,则打开网页,然后将页面信息保存为文本文件.如果没有url,那么它只是将文件中的文本保存为文本文件.我无法弄清楚如何更改语法以使其工作.

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
Run Code Online (Sandbox Code Playgroud)

Pat*_*rez 20

在第一行之前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
Run Code Online (Sandbox Code Playgroud)

  • 智能压头立刻突出了这个问题,因为`Else`没有与`If`对齐 (2认同)