Solved

Excel VBA

Posted on 2011-03-25
5
380 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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
All of your values are value = 0 !!!!!!
0
 
LVL 29

Expert Comment

by:leonstryker
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

728 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

10 Experts available now in Live!

Get 1:1 Help Now