Solved

Excel VBA

Posted on 2011-03-25
5
383 Views
Last Modified: 2012-05-11
Hello Experts,

I need some guidance on how to get my VBA code to select the correct sheet.  I am not sure if it should be a "case select" or "If statement".  I have tried a few things, but the wrong sheet is being selected.

I have 5 sheets in the workbook and they all validate Range"I4"

I need it to determine which sheet to select based on the value of thiscell.

If sheet5.Range("I4").value = 0 then
Sheet4.activate

Elseif sheet4.Range("I4").value = 0 then
Sheet3.activate

Elseif  sheet3.Range("I4").value = 0 then
Sheet2.activate

Elseif sheet2.Range("I4").value = 0 then
Sheet1.activate

End if

I am not sure what I am doing wrong - but the code is not activating the correct sheet.  I always want it to eliminate sheet5, then sheet4, then sheet3, then sheet 2  and finally go to sheet1 if all the others are eliminated - in that sequence. Do I need to resort the Else statements or is this a Case issue?

mike637


0
Comment
Question by:mike637
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35215429
If sheets(5).Range("I4").value = 0 then
Sheet4.activate

Elseif sheets(4).Range("I4").value = 0 then
Sheet3.activate

Elseif  sheets(3).Range("I4").value = 0 then
Sheet2.activate

Elseif sheets(2).Range("I4").value = 0 then
Sheet1.activate

End if
0
 

Author Comment

by:mike637
ID: 35215716

Forgot to add the last line in my original question:

    Else
    Sheet5.Activate

with the change of the code you supplied - I am getting sheet5 activated even though other sheets should be activated since they meet the criteria.

Can you offer additional help?
0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
ID: 35215770
You can only activate one at a time. Also, you're using ElseIF. If you want to check them all, you'll have to use separate Ifs. If <condition> Then means that it will test condition one. ElseIf <condition> Then means that, if the previous If was false, it will test this new If. If the previous was true, it will ignore.

If sheets(5).Range("I4").value = 0 then
Sheets(4).activate
End If
If sheets(4).Range("I4").value = 0 then
Sheets(3).activate
End If
If sheets(3).Range("I4").value = 0 then
Sheets(2).activate
End If
If sheets(2).Range("I4").value = 0 then
Sheets(1).activate
End if


Mind you, with this last one, only the last sheet will be active.
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 35216115
All of your values are value = 0 !!!!!!
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 35216139
Here is the code for the Select statement

Select Case Sheet5.Range("I4").Value
    Case 0
        sheet4.Activate
    Case 1
        Sheet3.Activate
    Case 2
        Sheet2.Activate
    Case 3
        Sheet1.Activate
    Case Else
        Sheet5.Activate
End Select
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
Batch Numbering 6 30
Excel formula to show $0.00 if negative number. 7 53
Getting rid of #VALUE! 7 22
Unprotect for adding content within a Text Box 2 27
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

839 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