If statement
Manage your personal finances in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 4 of 4

Thread: If statement

  1. #1
    New Member
    Join Date
    Mar 2002
    Posts
    13
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

     
    How would I write code that would not open the workbook if it is already open?

    if Workbooks.Open Filename:="P:trial5.xls" already open then
    exit sub
    else
    Workbooks.Open Filename:="P:trial5.xls"
    end sub

    Thanks
    Wrecker

  2. #2
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sunny, spring-like Hull
    Posts
    3,339
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    The following should work for you: -

    Dim wb As Workbook
    Dim WorkbookOpen As Boolean

    For Each wb In Workbooks
    If wb.Name = "Trial5" Then WorkbookOpen = True
    Next wb

  3. #3
    New Member
    Join Date
    Mar 2002
    Posts
    13
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Is this what I need if the "trial5" workbook is already open? If it is already open, I need to ignore the open command.

    Cant seem to make it work

    Thanks
    Wrecker

  4. #4
    MrExcel MVP Barrie Davidson's Avatar
    Join Date
    Feb 2002
    Location
    Winnipeg
    Posts
    2,330
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    This will work for you Wrecker:

    Dim wb As Workbook
    Dim WorkbookOpen As Boolean

    For Each wb In Workbooks
    If wb.Name = "Trial5" Then WorkbookOpen = True
    Next wb
    If WorkbookOpen = False Then Workbooks.Open Filename:="P:trial5.xls"

    Regards,

    Barrie Davidson

    "You're only given a little spark of madness. You mustn't lose it." - Robin Williams

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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

 

 
DMCA.com