Solved

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

Posted on 2013-01-02
6
187 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

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

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now