Open multi URLs in different tabs but same browser's window

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
I tried to have the code to open all URLs from different rows in a column but it only opens all of them in different windows. Only the URL(not listed in the rows) at the end of the script be opened in the same window of the URL listed in the last row. What should be the correct approach to achieve the goal ?


VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim lrow As Long
    Dim IE As Object
    Dim linkcell As Range
 
    lrow = Range("A1").End(xlDown).Row
    
    If Not Selection.Address = "$B$1" Then Exit Sub
        Cancel = True
        For Each linkcell In Range("E2:E" & lrow)
            Set IE = CreateObject("InternetExplorer.Application")
            IE.Visible = True
            IE.navigate linkcell
            
        While IE.readyState <> 4
          DoEvents
        Wend
        
        IE.Left = 0
        IE.Top = 0
        IE.Toolbar = 1
        IE.StatusBar = 1
        IE.Height = 600
        IE.Width = 720
        IE.resizable = True '(or False,1 or 0)

        Next linkcell
        
        Application.Wait (Now + TimeValue("00:00:03"))
        
        IE.navigate "https://stackoverflow.com/", CLng(2048)
        Set IE = Nothing
      
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi, please try this. There are some differences, and I used early-binding, so look in Tools > References for Microsoft Internet Controls & select that, or just replace it back with CreateObject if you prefer. The bottom line is that your subsequent iterations need to use .Navigate linkcell, CLng(2048), so you need an If statement. Basically the problem with your code is that you have the regular .Navigate inside a loop, so it will open a new window each time it hits that .Navigate linkcell, and the one at the end will just be an afterthought.

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim lrow As Long
    Dim IE As InternetExplorer
    Dim linkcell As Range
    Dim loopVar As Long
  
    lrow = Range("A1").End(xlDown).Row
    Set IE = New InternetExplorer
  
    If Not Selection.Address = "$B$1" Then Exit Sub
        Cancel = True
      
        For Each linkcell In Range("E2:E" & lrow)

            With IE
              
                If loopVar = 0 Then
                    .navigate linkcell
                Else
                    .navigate linkcell, CLng(2048)
                End If
              
                Do While .readyState <> 4
                  DoEvents
                Loop
              
                .Left = 0
                .Top = 0
                .Toolbar = 1
                .StatusBar = 1
                .Height = 600
                .Width = 720
                .resizable = True
                .Visible = True
              
            End With
            loopVar = loopVar + 1
          
        Next linkcell

        Set IE = Nothing
    
End Sub
 
Upvote 0
Hi Gravano, the code works. However I just learn that IE will be fading out after June. What is the workaround obj browser be changed to Chrome, Chromium Edge or Firefox instead but I do not want to install Selenium either.
I changed the code a bit that when the last url is opened, the browser will be maximized (actually I want the browser and excel windows to split side by side)

VBA Code:
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdSHow As Long) As Long
Const SW_SHOWMAXIMIZED = 3
Const SW_SHOWNORMAL = 1
Const SW_SHOWMINIMIZED = 2

'https://www.mrexcel.com/board/threads/open-multi-urls-in-different-tabs-but-same-browsers-window.1199584/#post-5854556
'#2 gravanoc

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim lrow As Long
    Dim linkcell As Range
    Dim loopVar As Long
    Dim IE As Object
    
    Set IE = CreateObject("Internetexplorer.application")
    
    lrow = Range("A1").End(xlDown).Row
 
    If Not Selection.Address = "$B$1" Then Exit Sub
        Cancel = True
      
        For Each linkcell In Range("E2:E" & lrow)

            With IE
              
                If loopVar = 0 Then
                    .navigate linkcell
                Else
                    .navigate linkcell, CLng(2048)
                End If
              
                Do While .readyState <> 4
                  DoEvents
                Loop
              
                .Left = 0
                .Top = 0
                .Toolbar = 1
                .StatusBar = 1
                .Height = 600
                .Width = 900
                .resizable = True
                .Visible = True
              
            End With
            loopVar = loopVar + 1
          
        Next linkcell
        
        IE.navigate "https://www.mrexcel.com/board/", CLng(2048)
        
        ShowWindow IE.hWnd, SW_SHOWMAXIMIZED
              
        Set IE = Nothing
 
End Sub
 
Upvote 0
As far as I know, Selenium is possibly the only alternative, but can't say for sure.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,173
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top