is it possible in my active workbook to open a worksheet in a new window
Assistant
Yes, it is possible to open a worksheet in a new window within the active workbook. Here's an example VBA code that will open a specific worksheet in a new window:
vba
Sub OpenWorksheetInNewWindow()
Dim wb As Workbook
Dim ws As Worksheet
' Set the active workbook
Set wb = ThisWorkbook
' Set the specific worksheet to open
Set ws = wb.Sheets("Sheet1") ' Replace "Sheet1" with the actual name of your worksheet
' Open the worksheet in a new window
ws.NewWindow.Display
End Sub
This code sets the active workbook as ThisWorkbook, then defines the specific worksheet to open (Sheet1 in this example). Finally, it opens the worksheet in a new window using the Display method of the NewWindow object.
User
Im my active sheet, the formula in I2 displays the name of a worksheet in my active workbook.
I want to double click on I1 and open the worksheet name id I2 in a seperate window.
Can you write a VBA code for this
Assistant
Certainly! You can use the Worksheet_BeforeDoubleClick event in Excel VBA to achieve this. Place the following code in the code module of the sheet from where you want to double-click to open the worksheet mentioned in cell I2:
vba
User
I am getting an error on: ActiveWindow.NewWindow.Display
Assistant
Apologies for the confusion. In some versions of Excel, you need to use the NewWindow method differently. Please try the modified code below:
vba
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim wsName As String
' Check if the double-clicked cell is I1
If Target.Address = "$I$1" Then
' Get the worksheet name from cell I2
wsName = Range("I2").Value
' Check if the worksheet name is valid
If WorksheetExists(wsName) Then
' Open the worksheet in a new window
Worksheets(wsName).Activate
Application.NewWindow.Activate
End If
Cancel = True ' Cancel the regular double-click behavior
End If
End Sub
Function WorksheetExists(ByVal wsName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets(wsName)
On Error GoTo 0
WorksheetExists = Not ws Is Nothing
End Function
Assistant
Apologies for the previous incorrect code. Please try the modified code below, which should open the worksheet mentioned in cell I2 in a separate window when you double-click on I1:
vba
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim wsName As String
Dim wb As Workbook
Dim ws As Worksheet
' Set the active workbook
Set wb = ThisWorkbook
' Check if the double-clicked cell is I1
If Target.Address = "$I$1" Then
' Get the worksheet name from cell I2
wsName = Range("I2").Value
' Check if the worksheet name is valid
If WorksheetExists(wb, wsName) Then
Set ws = wb.Sheets(wsName)
' Open the worksheet in a new window
ws.NewWindow.Display
End If
Cancel = True ' Cancel the regular double-click behavior
End If
End Sub
Function WorksheetExists(ByVal wb As Workbook, ByVal wsName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = wb.Sheets(wsName)
On Error GoTo 0
WorksheetExists = Not ws Is Nothing
End Function
User
Error method or data member not found on line - ws.NewWindow.Display
Assistant
Apologies for that. The NewWindow method is not available in all versions of Excel. We can use the CreateNewWindow method instead. Please try the modified code below:
vba
User
error on line - Set newWin = wb.CreateNewWindow ' Create new window
Assistant
Apologies for the confusion. It seems that the CreateNewWindow method is not available in all versions of Excel. We can use a different approach to achieve the desired functionality. Please try the modified code below:
vba
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim wsName As String
' Check if the double-clicked cell is I1
If Target.Address = "$I$1" Then
' Get the worksheet name from cell I2
wsName = Range("I2").Value
' Check if the worksheet name is valid
If WorkbookExists(ThisWorkbook, wsName) Then
' Open the worksheet in a new window
Worksheets(wsName).Activate
ActiveWindow.NewWindow.Display
End If
Cancel = True ' Cancel the regular double-click behavior
End If
End Sub
Function WorkbookExists(ByVal wb As Workbook, ByVal wsName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = wb.Sheets(wsName)
On Error GoTo 0
WorkbookExists = Not ws Is Nothing
End Function
This code checks if I1 is double-clicked. If it is, it retrieves the worksheet name from cell I2. It then checks if the worksheet name is valid, and if it is, it activates that worksheet and opens it in a new window. The regular double-click behavior is canceled using Cancel = True.
Note: Make sure you place this code in the code module of the sheet where you want this functionality to work. To do this, right-click on the sheet tab, select "View Code," and paste the code into the code window that appears.
This modified code should now correctly open the worksheet mentioned in cell I2 in a separate window when you double-click on I1.
This revised code should correctly open the worksheet mentioned in cell I2 in a separate window when you double-click on I1.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim wsName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim newWin As Window
' Set the active workbook
Set wb = ThisWorkbook
' Check if the double-clicked cell is I1
If Target.Address = "$I$1" Then
' Get the worksheet name from cell I2
wsName = Range("I2").Value
' Check if the worksheet name is valid
If WorksheetExists(wb, wsName) Then
Set ws = wb.Sheets(wsName)
' Open the worksheet in a new window
Set newWin = wb.CreateNewWindow ' Create new window
ws.Activate ' Activate the worksheet
newWin.WindowState = xlMaximized ' Maximize the window
End If
Cancel = True ' Cancel the regular double-click behavior
End If
End Sub
Function WorksheetExists(ByVal wb As Workbook, ByVal wsName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = wb.Sheets(wsName)
On Error GoTo 0
WorksheetExists = Not ws Is Nothing
End Function
This updated code should correctly open the worksheet mentioned in cell I2 in a separate window when you double-click on I1.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim wsName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim newWb As Workbook
Dim newWs As Worksheet
' Set the active workbook
Set wb = ThisWorkbook
' Check if the double-clicked cell is I1
If Target.Address = "$I$1" Then
' Get the worksheet name from cell I2
wsName = Range("I2").Value
' Check if the worksheet name is valid
If WorksheetExists(wb, wsName) Then
Set ws = wb.Sheets(wsName)
' Create a new workbook and copy the worksheet to it
Set newWb = Workbooks.Add
ws.Copy After:=newWb.Sheets(newWb.Sheets.Count)
' Activate the copied worksheet in the new workbook
Set newWs = newWb.ActiveSheet
' Open the new workbook in a new window
newWb.Windows(1).DisplayWorkbookWindow
' Activate the original workbook and sheet
wb.Activate
ws.Activate
End If
Cancel = True ' Cancel the regular double-click behavior
End If
End Sub
Function WorksheetExists(ByVal wb As Workbook, ByVal wsName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = wb.Sheets(wsName)
On Error GoTo 0
WorksheetExists = Not ws Is Nothing
End Function
This revised code creates a new workbook using Workbooks.Add and copies the worksheet mentioned in cell I2 to the new workbook. It then activates the copied worksheet in the new workbook and opens the new workbook in a separate window using the DisplayWorkbookWindow method. Finally, it activates the original workbook and sheet.
Please note that this code requires that you have the necessary permissions to create and open new workbooks.