MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 1st, 2002, 07:43 AM   #1
Ian Mac
MrExcel MVP
 
Join Date: Feb 2002
Location: Newcastle, UK
Posts: 1,174
Default

All,

I'm looking for a way to compact the following code:

Private Sub ShowFriday_Click()

Application.ScreenUpdating = False
Sheets("TeamTotalsFri").Visible = True
Sheets("James Third Fri").Visible = True
Sheets("Eleanor Bell Fri").Visible = True
Sheets("Alan Coombs Fri").Visible = True
Sheets("Andrew Hedley Fri").Visible = True
Sheets("Pete Walton Fri").Visible = True
Sheets("David Holliday Fri").Visible = True
Sheets("David Thornton Fri").Visible = True
Sheets("TeamTotalsFri").Select
Application.ScreenUpdating = True

End Sub

I have the same code for sheets mon - sun, currently I have 7 Buttons ShowMonday - ShowSunday.

I'd like to have 1 code which will unhide any sheets depanding on which button is pressed?

I also need the same thing for:

Private Sub CloseFriday_Click()
Application.ScreenUpdating = False
Sheets("TeamTotalsFri").Visible = False
Sheets("James Third Fri").Visible = False
Sheets("Eleanor Bell Fri").Visible = False
Sheets("Alan Coombs Fri").Visible = False
Sheets("Andrew Hedley Fri").Visible = False
Sheets("Pete Walton Fri").Visible = False
Sheets("David Holliday Fri").Visible = False
Sheets("David Thornton Fri").Visible = False
Sheets("WeeklySummarySheet").Select
Application.ScreenUpdating = True
End Sub

Which maybe easier as the button for these codes are on sheets named TeamTotalsFri etc.

Any pointers,

Thanks

Ian Mac

[ This Message was edited by: Ian Mac on 2002-03-04 03:04 ]
Ian Mac is offline   Reply With Quote
Old Mar 2nd, 2002, 02:26 PM   #2
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

Hi Ian Mac,

If the idea is to unhide all sheets whose names end with "Fri" then

Application.ScreenUpdating = False
Dim Sh As Sheet
For Each Sh In Sheets
If Right(Sh.Name,3)="Fri" Then
Sh.Visible = True
End If
Sheets("TeamTotalsFri").Select
Application.ScreenUpdating = True



__________________
Keep Excelling.

Damon

VBAexpert Excel Consulting
(My other life: http://damonostrander.com )
Damon Ostrander is offline   Reply With Quote
Old Mar 4th, 2002, 04:02 AM   #3
Ian Mac
MrExcel MVP
 
Join Date: Feb 2002
Location: Newcastle, UK
Posts: 1,174
Default

Quote:
On 2002-03-02 13:26, Damon Ostrander wrote:
Hi Ian Mac,

If the idea is to unhide all sheets whose names end with "Fri" then

Application.ScreenUpdating = False
Dim Sh As Sheet
For Each Sh In Sheets
If Right(Sh.Name,3)="Fri" Then
Sh.Visible = True
End If
Sheets("TeamTotalsFri").Select
Application.ScreenUpdating = True
Thanks Damon,

the you provided looks alright to me (but what do I know). I keep getting a:

User-defined type not defined

error, at the

Dim Sh as Sheet

line I changed the sheet to sheets and it goes past that line to

If Right(Sh.Name, 3) = "Fri" Then

and get a

Method or data member not found error.

it doesn't like the Name part,

Any suggestions?

Many thanks

__________________
"Have a good time......all the time"
Ian Mac
Ian Mac is offline   Reply With Quote
Old Mar 4th, 2002, 04:14 AM   #4
Mudface
MrExcel MVP
 
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
Default

Don't want to butt in but I think you could just change the Sheet to Worksheet or Object-

Dim sh as Worksheet

should be OK.
Mudface is offline   Reply With Quote
Old Mar 4th, 2002, 04:17 AM   #5
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi Ian

Call each of your buttons the same name as the Worksheet they hide/Unhide then assign them all to this code:

Sub WhichButton()
Dim strSheetName As String
strSheetName = Application.Caller
Sheets(strSheetName).Visible = Not Sheets(strSheetName).Visible
End Sub

This will toggle the visibility of the sheet in question.


Dave Hawley is offline   Reply With Quote
Old Mar 4th, 2002, 04:20 AM   #6
Ian Mac
MrExcel MVP
 
Join Date: Feb 2002
Location: Newcastle, UK
Posts: 1,174
Default

Quote:
On 2002-03-04 03:14, Mudface wrote:
Don't want to butt in but I think you could just change the Sheet to Worksheet or Object-

Dim sh as Worksheet

should be OK.
Not butting in at all....
Worksheet does the job but I now have to add a next somewhere, this something I've never used.

Many thanks


__________________
"Have a good time......all the time"
Ian Mac
Ian Mac is offline   Reply With Quote
Old Mar 4th, 2002, 04:22 AM   #7
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

BTW, if you do use the other helpers code try:

Option Compare Text
Sub AnotherWay()
Dim Sh As Worksheet
Application.ScreenUpdating = False

For Each Sh In ThisWorkbook.Worksheets
If Sh.Name Like ("* Fri") Then
Sh.Visible = True
End If
Sheets("TeamTotalsFri").Select
Application.ScreenUpdating = True

End Sub
Dave Hawley is offline   Reply With Quote
Old Mar 4th, 2002, 04:31 AM   #8
Mudface
MrExcel MVP
 
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
Default

Hehe, a surfeit of ways to do it

Just add the Next after the End If statement. You don't need to use Next sh, but it's a good habit to get into, as it makes your code more readable.
Mudface is offline   Reply With Quote
Old Mar 4th, 2002, 05:10 AM   #9
Ian Mac
MrExcel MVP
 
Join Date: Feb 2002
Location: Newcastle, UK
Posts: 1,174
Default

Quote:
On 2002-03-04 03:17, Dave Hawley wrote:
Hi Ian

Call each of your buttons the same name as the Worksheet they hide/Unhide then assign them all to this code:

Sub WhichButton()
Dim strSheetName As String
strSheetName = Application.Caller
Sheets(strSheetName).Visible = Not Sheets(strSheetName).Visible
End Sub

This will toggle the visibility of the sheet in question.


Thanks for all the input guys,

I'm currently using Damon's code amended by Mudface (Thank-you both), although I do like the I'd of using one code for all, the problem being that there are a lot of sheets ending in the day (see original post) and I can't see how the above code would handle lots of sheets.

I have re-named all the buttons to Mon - Fri rather than the ShowMonday etc. I had.
How can I use either code to achieve this??

Again thank-you so far, Fantastic work out there.



[ This Message was edited by: Ian Mac on 2002-03-04 04:13 ]
Ian Mac is offline   Reply With Quote
Old Mar 4th, 2002, 05:19 AM   #10
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Just name the buttons "TeamTotalsFri" etc. Don't confuse the name with the Caption though! To name a button from the Forms toolbar simply select it then type the name in the name box (to left of Formula bar) just as you would a named range.

Then simply assign ALL your buttons to this one tiny Procedure:

Sub WhichButton()
Dim strSheetName As String
strSheetName = Application.Caller
Sheets(strSheetName).Visible = Not Sheets(strSheetName).Visible
End Sub
Dave Hawley is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 02:45 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes