gpt4 book ai didi

VBA:否则没有如果错误

转载 作者:行者123 更新时间:2023-12-05 01:30:34 25 4
gpt4 key购买 nike

我正在尝试运行以下代码,但我在第一个 sub 中不断获得没有 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

最佳答案

与第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

关于VBA:否则没有如果错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15251829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com