Compact if statements

Jetx227

Board Regular
Joined
Jul 11, 2018
Messages
96
Hi guys! So I need a little help seeing if there's a way compacting some code down because I don't want to go through every combination of if statements if they are possible to be avoided, but my vba knowledge (and formula knowledge) is limited. So basically, I have this table (here's an example of how it could look):
Item #Year 1Year 2Year 3Year 4Year 5Total (Formula Driven)
10143.5321See formula below
102
103
104
105
106
107
108

I just filled out one sample set and I'll explain how it works. So this table is a specific data points pulled from another sheet (not important to this), but each year gets its own "rating" of sorts, rated from 1-4 (these could be decimal number, doesn't have to be a whole number) The maximum number of years of data is 5 and the minimum number of years is 1. The total column is a weighted formula that we used to give us a number we use later on, but recently we've been having some issues getting it. So for five years, our formula looks like this (below, note that Rate1, 2, 3... is the Rating in Year 1, 2, 3... (rated 1-4) and the Year variable is just the year integer).
Formula (note that right now it's in VBA in my workbook, for a 5 year rating):
VBA Code:
= (((Rate1 - Rate2) * 4) + ((Rate2 - Rate3) * 3) + ((Rate3 - Rate4) * 2) + (Rate4 - Rate5)) / (Year1 - Year5)
Formula (note that right now it's in VBA in my workbook, for a 4 year rating):
VBA Code:
= (((Rate1 - Rate2) * 3) + ((Rate2 - Rate3) * 2) + (Rate3 - Rate4)) / (Year1 - Year4)
Hopefully you see the pattern we use for the formula now.
This works fine so long as all the ratings are 1-4 as they should be. But now, for a few items we don't have the data for a few years, so they are currently inputted as a 0. What we are trying to do, is get it so that if a rating is a 0, to replace it with the previous year's rating. So in the table above for example, if the "Year 4" rating was a 0, it would be replaced by Year 5's rating instead. (Note that if year 5's rating is zero, since we don't have previous data, we just ignore it and use a 4 year formula instead.) We also want this to work if there are multiple 0's (2 to 3) so that it pulls from the previous years rating. If there is only 1 rating or no ratings that are 1-4 then the formula returns 0. Hopefully I've described this clearly, but if there are any questions or clarifications please let me know! And thanks in advance for any help!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Say the above is A1:G9, in I2 i'd put =IF(B2=0,J2,B2). Then copy that in I2:L9. Then = (((I2 - J2) * 4) + ((J2 - K2) * 3) + ((K2 - L2) * 2) + (L2 - M2)) / (I2 - M2)

(would need something else for year 5

1583374984762.png
 
Upvote 0
Say the above is A1:G9, in I2 i'd put =IF(B2=0,J2,B2). Then copy that in I2:L9. Then = (((I2 - J2) * 4) + ((J2 - K2) * 3) + ((K2 - L2) * 2) + (L2 - M2)) / (I2 - M2)

(would need something else for year 5

View attachment 8253
Great! this works perfect for a 5 year scenario (its similar to what I'm using, I'm just using code to do all my stuff), is there a way to make it dynamic so that it would auto switch the total formula if its a 4, 3 or 2 year scenario? Basically, I have code that pulls all the data from a sheet, fills it into this table and then calculates it. Once its done, it copies the total back to the original sheet and then goes to the next sheet. So each sheet could have a random number of years. Right now I have all these nested if statements that take up the majority of my code. It works but it's just tough to follow and know where a typo in a formula is occurring because there's so many lines to look through.
 
Upvote 0
If this is the 5 year formula.

= (((I2 - J2) * 4) + ((J2 - K2) * 3) + ((K2 - L2) * 2) + (L2 - M2)) / (I2 - M2)

then i'd just use:

=IF(D2="",2 year formula,IF(E2="",3 year formula,IF(F2="",4 year formula,5 year formula)))

=IF(D2="",2 year formula,IF(E2="",3 year formula,IF(F2="",4 year formula,(((I2 - J2) * 4) + ((J2 - K2) * 3) + ((K2 - L2) * 2) + (L2 - M2)) / (I2 - M2))))
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top