编译错误参数不是可选的

时间:2017-12-07 21:38:26

标签: excel vba

我之前在一台临时计算机上运行过的代码运行得很完美,但即使我参考了作者的建议,我也无法让它在这台机器上运行

我得到了

  

编译错误参数不是可选的

on Sub kl()

Sub kl()
Dim ie As InternetExplorer
Dim htlm As HTMLDocument
Dim link As Object
Dim links As Object
Dim erow As Long
Application.ScreenUpdating = False
Set ie = New InternetExplorer
ie.Visible = False
ie.navigate = "http://www.google.com"

Do While ie.ReadyState <> READYSTATE_COMPLETE
    Application.StatusBar = "loading website"
    DoEvents
Loop

Set Html = ie.document
Set links = Html.getElementsByTagName("a")
For Each link In links
    erow = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    Cells(erow, 1).Value = link
    Cells(erow, 1).Columns.AutoFit
Next

Set.ie = Nothing
Application.StatusBar = ""
Application.screenupdate = True

End Sub

有关导致错误原因的任何帮助?我已经引用了MSXML,ActiveX库,MSHTML和MS Internet Controls

我收到了ie.navigate =“http://www.google.com

的错误

工作代码:

Sub kl()
Dim ie As Object
Dim htlm As HTMLDocument
Dim link As Object
Dim links As Object
Dim erow As Long

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.navigate "http://www.google.com"


Do While ie.ReadyState <> READYSTATE_COMPLETE
Application.StatusBar = "loading website"
DoEvents
Loop

 Set HTML = ie.document
 Set links = HTML.getElementsByTagName("a")
For Each link In links
erow = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 1).Value = link
Cells(erow, 1).Columns.AutoFit
Next

Set ie = Nothing
Application.StatusBar = ""


End Sub

1 个答案:

答案 0 :(得分:1)

我认为错误在于您如何创建和调用Internet Explorer应用程序。

请改为尝试:

...
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.navigate "http://www.google.com"  ' This seems to be the main thing, don't use `=`
...