Solved

Macro with "If" Statement for choosing which Macro to run

Posted on 2013-01-02
6
224 Views
Last Modified: 2013-01-06
EE Pros;

I have three tabs on in a Workbook -- Sheets 1, 2 and 3.  Only Sheet 1 is visable; the other two are hidden.  I have built a Macro on Sheet 1 that will "expose" EITHER Sheet 2 or 3 but not both at the same time.  Here is my challenge....;  I need a Macro that can be fired from Sheets 2 OR 3 that will hide them and return you to Sheet 1 (.Active).  What I don't know is how to use an "if" statement in a macro, and what do I use to identify if it's Sheet 2 or Sheet 3 that is exposed (the Sheet name?)?  I would envision something like;

=if("Sheet2",xlvisibility=.hidden,Sheet3.xlvisibility=.hidden)
.activate "Sheet1"

But only as a "macro newbee"!

B.
0
Comment
Question by:Bright01
  • 2
  • 2
  • 2
6 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 38736489
How are you triggering the macro? If it's from the sheet in question, then you can simply refer to activesheet.
0
 

Expert Comment

by:ChrisKader
ID: 38736494
Sub Hide()

ActiveSheet.Visible = xlSheetHidden

End Sub
0
 

Author Comment

by:Bright01
ID: 38736522
I do apologize.  I have this backwards.  

When the model starts up, I have a single Sheet appear (Sheet1, all others are hidden).  Based on a selection, a macro fires that brings up EITHER Sheet 2 or Sheet 3. From Sheet 2 or 3, I have a macro at the top of each Sheet (in a module because either Sheet can use it) that exposes Sheet 4.  My problem is in closing Sheet 4 and returning to EITHER Sheet 2 or 3 depending on which one is open.  I need Sheet 4 to determine if Sheet 2 or Sheet 3 is open and that is the Sheet to return to while hiding itself.

Make sense?  Again, sorry for the initial confusion.

B.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Assisted Solution

by:ChrisKader
ChrisKader earned 250 total points
ID: 38736536
Assuming only one will be visible at any given time:

Sub HideCheck()

If Sheet2.Visible = True Then
    ActiveSheet.Visible = False
    Sheet2.Activate
Else
    ActiveSheet.Visible = False
    Sheet3.Activate
End If

End Sub

Open in new window

0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 250 total points
ID: 38736593
Life would be much simpler if you posted the code you were using. ;)

You could simply set a variable in the macro that makes sheet4 visible, and then hide sheet 4 and reactivate that sheet using the variable.
Public ws as Worksheet

Open in new window


then your macro does
Sheet2.visible = xlsheetvisible
set ws = sheet2

Open in new window


and the hide code simply uses
sheet4.visible = xlsheethidden
ws.activate

Open in new window

0
 

Author Closing Comment

by:Bright01
ID: 38749218
Thanks guys.  Very sorry for the delay..... I have had the worst case of the flu that I think I have ever had.  Anyway, getting back into it and your solution(s) worked perfectly.

Again, much thanks!

B.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

685 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