Solved

Activate Workbook and Worksheet with Wildcard

Posted on 2011-09-30
6
2,584 Views
Last Modified: 2012-05-12
I need to activate a workbook and worksheet that will already be open. I have code that works for this for the specific name, but the date at the beginning will change every month, so I want code that will use a wildcard for the date and select the workbook and worksheet for activation just using the last part of the name.

Example:
This is what works now:
Workbooks("2011_09_Name_of_Workbook.xlsm").Sheets("2011_09_Worksheet_Name").Activate

This is the basic idea of what I would like to work:
Workbooks("*_Name_of_Workbook.xlsm").Sheets("*_Worksheet_Name").Activate

Thank you
0
Comment
Question by:AndresHernando
  • 5
6 Comments
 
LVL 33

Accepted Solution

by:
jppinto earned 500 total points
ID: 36890509
You can try something like this:
Dim wkb As Workbook
    For Each wkb In Workbooks
        If wkb.Name Like "*_Name_of_Workbook.xlsm" Then
            wkb.Activate
            Exit For
        End If
    Next wkb

Open in new window

0
 

Author Comment

by:AndresHernando
ID: 36890584
jpp,

I tried code like that before posting, but it didn't work. But I figured I'd give yours a try again anyhow. It still doesn't work. It seems to skip over the "wkb.activate" command when I step through it. It will loop a couple of times, and then just move on the the next portion of code. It doesn't crash or give an error msg, but it doesn't activate the workbook and worksheet that I want to be activated.

Thanks
0
 

Author Comment

by:AndresHernando
ID: 36890594
jpp,

Nevermind that last post. I forgot to change the "*_Name_of_Workbook.xlsm" back to the proper name. Once I changed it to the proper name, it loops through 3 times, and on the 3rd try it activates like it is supposed to. Not sure why it failed on me before? But it works now.

Thanks!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Closing Comment

by:AndresHernando
ID: 36890595
It worked
0
 

Author Comment

by:AndresHernando
ID: 36890613
Oh, I didn't notice until I tried to run it again that the code didn't call the spreadsheet within the workbook too. Do you know how I could call the spreadsheet within this workbook? To make matters more difficult, other open workbooks (with different names) will have a worksheet in them with the same name. That's why I need to make sure that it only calls the sheet within the active workbook.
0
 

Author Comment

by:AndresHernando
ID: 36890617
Nevermind that last one, I got it.

Thanks again.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

760 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

18 Experts available now in Live!

Get 1:1 Help Now