Solved

How to reference an open workbook using wildcard in VBA

Posted on 2007-03-29
4
1,260 Views
Last Modified: 2012-08-14
Hi all,

How do I activate a workbook (which is already open within the current application) for which I know part of the name? Like, I know that the workbook starts with: CMVINXBXINNNI_Index_MorningReport-
but the rest of the name is unknown (or will vary).

Example:
Windows("CMVINXBXINNNI_Index_MorningReport-20070329").Activate

How come I can not reference this workbook in the following manner:
Windows("CMVINXBXINNNI" & "*").Activate

Hope you can help :)
0
Comment
Question by:WileyPowers
4 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 250 total points
ID: 18814777
Hi WileyPowers,

You can't use wildcards to reference a workbook, because there may be multiple workbooks with a similar name. You could use this function....

     Public Function GetWorkbook(strLike As String) As Workbook
          Dim wb As Workbook
          For Each wb In Application.Workbooks
               If wb.Name Like strLike Then
                    Set GetWorkbook = wb
                    Exit Function
               End If
          Next
     End Function

...and you'd use it like this....

     GetWorkbook("CMVINXBXINNNI" & "*").Activate

Note: If there are multiple workbooks with a similar name, the function will return the first one it finds.

Regards,

Wayne
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now