Solved

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

Posted on 2013-01-02
6
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Industry Leaders: 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!

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

734 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