+ Reply to Thread
Results 1 to 5 of 5

open worksheet only if not already open"

Hybrid View

  1. #1
    Registered User
    Join Date
    03-28-2013
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    18

    open worksheet only if not already open"

    i have made two worksheet where one worksheet is linked with other whenever i open 1 worksheet the other worksheet open ...so i want that if the other worksheet open already than dont open again and dont show me the msg " that the other worksheet already open. do u want to reopen again. Yes no"


    please help me out of this problem

    thanks in advance

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: open worksheet only if not already open"

    Hi, vipulhumein,

    maybe like this (code could be accessible in ThisWorkbook and Private Sub Workbook_Open):
    Sub EF912816()
    Dim wb As Workbook
    Dim wbONE As Workbook
    Dim wbTWO As Workbook
    
    Const cstrWB_ONE As String = "Raw Data.xlsm"      'alter to suit name and extension
    Const cstrWB_TWO As String = "Sample.xlsx"        'like above
    Const cstrPATH As String = "C:\HaHoBe\Downloads\" 'alter the path to the workbooks, I
                                                      'assumed they both are located in the same place
    
    For Each wb In Workbooks
      If wb.Name = cstrWB_ONE Then
        Set wbONE = wb
      End If
      If wb.Name = cstrWB_TWO Then
        Set wbTWO = wb
      End If
    Next wb
    
    If wbONE Is Nothing Then
      Set wbONE = Workbooks.Open(cstrPATH & cstrWB_ONE)
    End If
    
    If wbTWO Is Nothing Then
      Set wbTWO = Workbooks.Open(cstrPATH & cstrWB_TWO)
    End If
    
    End Sub
    Work with the workbook objects wbONE and wbTWO later in the code instead of switching Windows.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    03-28-2013
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: open worksheet only if not already open"

    it doesn't work after applying in this workbook .... now also the same msg came " that u have already open the other workbook, do u want to reopen again"

    please help me out of this

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: open worksheet only if not already open"

    Hi, vipulhumein,

    it doesn't work after applying in this workbook
    Where´s the code you used? I can´t look into your computer I´m afraid.

    The code assumes that you open the workbooks in the same instance of Excel, not in different ones.
    'This Workbook
    Option Explicit
    
    Private Sub Workbook_Open()
    EF912816
    End Sub
    'Code Module
    Option Explicit
    
    Sub EF912816()
    Dim wb As Workbook
    Dim wbONE As Workbook
    Dim wbTWO As Workbook
    
    Const cstrWB_ONE As String = "Raw Data.xlsm"      'alter to suit name and extension
    Const cstrWB_TWO As String = "Sample.xlsx"        'like above
    Const cstrPATH As String = "C:\HaHoBe\Downloads\" 'alter the path to the workbooks, I
                                                      'assumed they both are located in the same place
    
    For Each wb In Workbooks
      If wb.Name = cstrWB_ONE Then
        Set wbONE = wb
      End If
      If wb.Name = cstrWB_TWO Then
        Set wbTWO = wb
      End If
    Next wb
    
    If wbONE Is Nothing Then
      Set wbONE = Workbooks.Open(cstrPATH & cstrWB_ONE)
    End If
    
    If wbTWO Is Nothing Then
      Set wbTWO = Workbooks.Open(cstrPATH & cstrWB_TWO)
    End If
    
    End Sub
    Ciao,
    Holger

  5. #5
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: open worksheet only if not already open

    Function IsWbOpen(wbName As String) As Boolean
        Dim i As Long
        For i = Workbooks.Count To 1 Step -1
            If Workbooks(i).Name = wbName Then
                Set ArchiveBook = Workbooks.Item(i)
                Exit For
            End If
        IsWbOpen = False
        Next
    End Function
    
    Sub OpenWb()
        If IsWbOpen("Exel Forum.xls") = False Then
        'Your code here
        End If
    End Sub
    Elegant Simplicity............. Not Always

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1