Solved

Excel VBA

Posted on 2011-03-25
5
385 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
[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
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

Technology Partners: 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 code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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 will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

730 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