Solved

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

Posted on 2013-01-02
6
234 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

623 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