VBA:在IE上的新选项卡上打开一个网站,然后等待它加载



我最近学会了通过VBA excel在IE上打开网站。现在我正在尝试添加代码,该代码将检查活动的IE是否已打开,如果是,它将打开一个新选项卡以浏览网站并再次执行相同的操作。不幸的是,我尝试过的代码不起作用。我做错了什么?

Sub OpenIE()
Dim IE As Object
Dim site As String
Set IE = CreateObject("InternetExplorer.Application")
site = "https://www.automateexcel.com/excel/"
IE.Visible = True
IE.navigate site
Application.StatusBar = site & " " & "is loading"
Do While IE.readyState = 4: DoEvents: Loop
Do Until IE.readyState = 4: DoEvents: Loop
Application.StatusBar = site & " " & "is loaded"
Set IE = Nothing
End Sub

这是以编程方式在IE对象中打开多个选项卡的方式

Sub Test()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Navigate "https://amazon.com" '1st tab
        .Navigate "https://flipkart.com", CLng(2048) '2nd
        .Navigate "https://snapdeal.com", CLng(2048) '3rd
        .Visible = True
    End With
End Sub

这也有效

Sub Test()
Dim IE As Object
Set IE = Nothing
Set IE = CreateObject("InternetExplorer.Application")
Set cell = Cells(1, 1)
For i = 1 To 5
IE.navigate "https://www.google.com/search?q=" & Cells(i, 1) & "&rlz=1C1CHBF_enUS828US828&oq=62654962004&aqs=chrome..69i57j69i59.95j0j4&sourceid=chrome&ie=UTF-8", CLng(2048)
IE.Visible = True
Next
End Sub

最新更新