?
Solved

Excel VBA

Posted on 2011-03-25
5
Medium Priority
?
392 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 2000 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

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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.

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