Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making a sheet active How-To ...

Status
Not open for further replies.

wolfess

Programmer
Oct 1, 2002
4
US
Desired behaviour: Activate a particular sheet in a workbook based on its name.

I've been using the search feature of this forum and found this code snippet.

Sub test()
For Each sht In ActiveWorkbook.Worksheets
"Your code"
Next sht
End Sub

So, to accomplish the above desired behaviour would the below modification to the snippet be correct?

Sub test()
For Each sht In ActiveWorkbook.Worksheets
sht.activate
if activesheet.name = "Sep 05"
Exit
endif
Next sht
End Sub

Also, is there a way to enumerate the sheets without activating them and activate only when the correct sheet is found?

As always, TIA

Steve

 
no need to enumerate:

Sheets("SheetName").activate

Almost all objects in excel can be used like this - there is no need to enumerate through collections

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 



Hi,

I think what xlbo might have meant to say was, no need to Activate each sheet...
Code:
Sub test()
For Each sht In ActiveWorkbook.Worksheets
  if sht.name = "Sep 05" then
     sht.Activate
     Exit sub
  endif
Next sht
End Sub
How Can I Make My Code Run Faster? faq707-4105

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
Why not simply this ?
ActiveWorkbook.Worksheets("Sep 05").Activate

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Both the Sheets("SheetName").Activate and the
ActiveWorkbook.Worksheets("Sep 05").Activate
methods work.

Direct access via a method is always better than enumerating an object. I have put in a request for the book that was suggested earlier that lays out the Excel Object Model -- that will help a lot. Without knowing the object's methods and properties addressable names or legal aliases it is kind of hard to get things to work.

Thanks a bunch to all contributors, your help is very much appreciated.

Steve
 
that lays out the Excel Object Model
I personally only play with the F2 (object browser) and F1 (VBA help) keys.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
seconded - a quick press of the F2 key in the VBE will give you the object model

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top