Consulting

Results 1 to 8 of 8

Thread: Solved: Open file .xls or .xlsm, or xlsm

  1. #1
    VBAX Regular
    Joined
    Mar 2011
    Posts
    92
    Location

    Solved: Open file .xls or .xlsm, or xlsm

    First off all thanks to all members of this forum extremely helpful

    This is the problem:

    In my code i need to activate a workbook whit a fixed name but variable suffix, it can be xls, xlsm or xlsb

    This is the code for xlsm suffix: Workbooks("Rapportini Cantieri.xlsm").Activate

    What is the way for activate the workbook with any suffix?

    Thanks in advance
    Last edited by Rayman; 06-10-2011 at 03:58 PM.

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    In your thread title, you refer to "Open file," but your code shows activating a workbook. Is the workbook already open, or are we first opening it?

  3. #3
    VBAX Regular
    Joined
    Mar 2011
    Posts
    92
    Location
    Quote Originally Posted by GTO
    In your thread title, you refer to "Open file," but your code shows activating a workbook. Is the workbook already open, or are we first opening it?
    Sorry my mistake, the workbook is alredy open i need switch to one workbook to another.
    In my code i use the xlsm extension, but my goal is to make my code working with any extension.

    ( if i saved my workbook with xlsb extension, i need to change all the reference to xlsm in my code)

    Thanks for reply

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Presuming you mean any workbook extension, maybe:
    Option Explicit
        
    Sub exa()
    Dim wb As Workbook
    Dim MyBook As Workbook
        
        For Each wb In Workbooks
            If wb.Name Like "MyBook.xls*" Then
                Set MyBook = wb
                Exit For
            End If
        Next
        
        If Not MyBook Is Nothing Then
            MyBook.Activate
        End If
    End Sub

  5. #5
    VBAX Regular
    Joined
    Mar 2011
    Posts
    92
    Location
    Thanks GTO for reply,
    Ill try to integrate your code with mine and il let you now.

    Thanks again

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    you might need to check to make sure it's not an Add In or that it's hidden

    [VBA]
    For Each wb In Workbooks
    If wb.Name Like "MyBook.xls*" Then
    'might need these checks also
    If Not wb.IsAddin And wb.Windows(1).Visible Then
    Set MyBook = wb
    Exit For
    End If
    endf
    Next
    [/VBA]

    Paul

  7. #7
    VBAX Regular
    Joined
    Mar 2011
    Posts
    92
    Location
    Thanks Paul,

    You are right, check is needed.

    Ill assembly your and GTO's code.

  8. #8
    VBAX Regular
    Joined
    Mar 2011
    Posts
    92
    Location
    Ok Paul and GTO, all is done and work perfectly.

    Many many thanks

    Mario

Posting Permissions

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