Solved

Help with a long if then else statement in Excel using VBA

Posted on 2012-03-26
10
335 Views
Last Modified: 2012-06-21
I am hoping this will be decently easy for you experts!! I have an excel formula that already exists in my spreadsheet. The problem that I am having is that I need to add quite a bit more logic to it. I have a variable named months this is tied to a spreadsheet in the workbook.. "bookmarks" is the name. Here is the formula now..

=IF(ISNA(VLOOKUP(Years,Bookmarks!C39:D43,2)),1,VLOOKUP(Years,Bookmarks!C39:D43,2))

I need to add to this formula

If month (- this is the variable name) =

0 to 11     then 0
12 to 18   then 1
19 to 30   then 1.5
31 to 42   then 2
43 to 54   then 2.5
55 to 60   then 4

can someone help me with this?? Pretty Please??
0
Comment
Question by:Jennifer Barman
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 9

Expert Comment

by:OCDan
ID: 37768155
Do you have some sample data to look at? And what you would expect the result to be given certain data?

Sorry for the questions I am just pretty unclear on what you are after here.
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 37768247
I see you're returning "1" if the VLOOKUP fails.  

Are now wanting to return one of the possible corresponding values in your list that depends on the value for the variable "months"?

If so, the formula would be simpler (and easier to edit) if you had another lookup list to use for "months" and its lookup value.

-Glenn
0
 

Author Comment

by:Jennifer Barman
ID: 37768256
well this populates to a cell.. and is figured into a  calculation after the fact.

I am not extreamly familiar with vlookup or how the first statement was made. All I need to do is add the information below to the formula bar.. something like a nested if then??

Not really sure what you are wanting for example data. Right now it is creating a multiplier on our commissions sheet.
0
 

Author Comment

by:Jennifer Barman
ID: 37768271
I actually have created a list in bookmarks.. I guess I am not sure how to write this out?? I can give you the cell names? first the list also comes from bookmarks.. the cell range is P16:P21
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 37768319
Okay, if P16:P21 is the list for the month range, you need to have the appropriate multipliers adjacent to them.  Like so:

P16 = 0
P17 = 12
P18 = 19
P19 = 31
P20 = 43
P21 = 55

Then, the multipliers
Q16 = 0
Q17 = 1
Q18 = 1.5
Q19 = 2
Q20 = 2.5
Q21 = 4

Then, replace the "1" in your original formula (the "True" condition of your IF statement) like so:

=IF(ISNA(VLOOKUP(Years,Bookmarks!C39:D43,2)),VLOOKUP(months,$P$16:$Q$21,2,TRUE),VLOOKUP(Years,Bookmarks!C39:D43,2))

Of course, I'm still assuming you're replacing the error value of "1" with the multiplier based on the months value.  If that's incorrect, you'll need to further clarify what you want to do.

-Glenn
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37768365
You can use this formula without using a range

=VLOOKUP(month,{0,0;12,1;19,1.5;31,2;43,2.5;55,4},2)

if you need a not found condition you can use

=if(isna(VLOOKUP(month,{0,0;12,1;19,1.5;31,2;43,2.5;55,4},2)),1,VLOOKUP(month,{0,0;12,1;19,1.5;31,2;43,2.5;55,4},2))

or in 2007 or later

=iferror(VLOOKUP(month,{0,0;12,1;19,1.5;31,2;43,2.5;55,4},2),1)
0
 

Author Comment

by:Jennifer Barman
ID: 37768620
I tried the above query (Glenn).. following your directions.. but it now jsut say #N/A.. :(

any ideas?

ssaquhb... can you figure out how to add my existing formula with my new data.. I have to have the old data in the collection.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 37768673
CJ,

In any blank cell, insert the following formula:

=VLOOKUP(months,Bookmarks!$P$16:$Q$21,2,TRUE)

If the range/variable "months" is not blank and is a number, AND if you populated the range of P16:Q21 as I noted above, you will get a result.  The "Bookmarks!" part here may be what is missing.

-Glenn
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37769364
=IF(ISNA(VLOOKUP(Years,Bookmarks!C39:D43,2)),1,VLOOKUP(Years,Bookmarks!C39:D43,2))+if(isna(VLOOKUP(month,{0,0;12,1;19,1.5;31,2;43,2.5;55,4},2)),1,VLOOKUP(month,{0,0;12,1;19,1.5;31,2;43,2.5;55,4},2))
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 37772665
Just looking at some of the solutions provided and I notice that only Glenn's comment 37768673 has the TRUE/FALSE indicator for the lookup formula.

In theory without the indicator the lookup will never fail because it will return the closest match therefore the IF(ISNA(VLOOKUP...) will always go to the second option as the ISNA condition will not occur.

Thanks
Rob H
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
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…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
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.

757 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

21 Experts available now in Live!

Get 1:1 Help Now