我想運行下面的代碼,但我不斷收到沒有如果錯誤在第一個子。代碼應該在列中運行,如果單元格中有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
你可以在你的代碼中修復回車嗎?很難判斷遺漏回車是否是問題,或者是由於您粘貼代碼的方式引起的。 – Keith 2013-03-06 15:55:26
我可以建議您安裝並使用智能縮進(http://www.oaltd.co.uk/Indenter/)還是努力讓正確縮進而不是混亂? – 2013-03-06 16:04:08