Consulting

Results 1 to 3 of 3

Thread: Get Workbook Index from VBA

  1. #1
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location

    Get Workbook Index from VBA

    Hello,

    I know that you can use
    [VBA]Workbooks.Item(l)
    'or
    Workbooks(l)[/VBA]
    to set reference to a specific Workbook. But the Workbook object hasn't the Item (neither Index) property. Is there a way to get directly a Workbook's index instead of using my workaround below?
    [VBA]Sub Exemplo()

    Debug.Print ÍndiceWB(ThisWorkbook)

    End Sub

    Private Function ÍndiceWB(wb As Workbook) As Long
    Dim l As Long

    For l = 1 To Workbooks.Count
    If Workbooks(l).FullName = wb.FullName Then
    ÍndiceWB = l
    Exit Function
    End If
    Next l
    End Function[/VBA]
    ---
    Felipe Costa Gualberto
    Microsoft Excel MVP
    http://www.ambienteoffice.com.br

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I've never used Index with workbooks. I'd rather use variables set to the named book to avoid possible confusion.
    For the current book you can simply use
    [VBA]Set wb = ThisWorkbook
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    It is hard to explain, by I need this property on my project. I think I'll just keep using my custom function.
    ---
    Felipe Costa Gualberto
    Microsoft Excel MVP
    http://www.ambienteoffice.com.br

Posting Permissions

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