Hullo Excel Forum community!
I've currently got an inelegant, manual, error-prone solution to a gnarly problem that I would appreciate a dynamic and automatic solution. As you'll see, I'm still a teeny noob when it comes to Excel, and I would sincerely appreciate any help I can get. Thank you so much!
Setup
I currently have 1 worksheet called 'Inputs-Yearly' that houses all of the inputs that change yearly. For example:
B2 = price of diesel in 2013, C2 = price of diesel in 2014, D2 = price of diesel in 2015, etc.
B3 = price of gas in 2013, C3 = price of gas in 2014, D3 = price of gas in 2015, etc.
There are a total of 6 years (so columns B, C, D, E, F, G) and ~100 types of inputs (so rows 2, ..., 101)
My current setup has 1 worksheet per year, totaling 6 worksheets. Each worksheet would call inputs from only one column of the 'Inputs-Yearly' tab. I start off with one worksheet for year 1, I plan on copying that worksheet 5 times, and doing a manual Find and Replace for all instances of 'Inputs-Yearly'!$B > 'Inputs-Yearly'!$C for year 2 (and D, E, F, G for years 3 through 6, respectively). This is an inelegant solution
Ideal solution
1. Not require me to manually Find+Replace; the input values would dynamically update in accordance to the year.
2. Not have 6 copies of the same worksheet but 1 as any changes made to one I would have to copy over to the other five.
3. Not have an unwieldy 2-line formula every time I call a cell reference to the input.
My attempts
Failed Solution #1:
Dynamically update the year through CHOOSE, INDIRECT, CONCATENATE, and OFFSET.
In L6 is 1 (the year #)
In L7 is $C$2 (written as text, not cell reference; 'Input-Yearly'!$C$2 is the input from Year 1 I've called 800+ times in the worksheet for year 1)
In L8 is =CHOOSE($L$6,INDIRECT(CONCATENATE("'Inputs-Yearly'!",L7)),OFFSET(INDIRECT("'Inputs-Yearly'!",L7),0,1,1,1),OFFSET(INDIRECT("'Inputs-Yearly'!",L7),0,2,1,1),OFFSET(INDIRECT("'Inputs-Yearly'!",L7),0,3,1,1),OFFSET(INDIRECT("'Inputs-Yearly'!",L7),0,4,1,1),OFFSET(INDIRECT("'Inputs-Yearly'!",L6),0,5,1,1),OFFSET(INDIRECT("'Inputs-Yearly'!",L7),0,6,1,1))
This does dynamically update the year. However, there are ~100 types of inputs (C2, C3, C4, C5, all the way to C100) and I'd have to do a Find+Replace 100x.
Failed Solution #2:
Run a Find+Replace:
Find: 'Inputs-Yearly'!$C$2
Replace: INPUTFORYEAR($C$2)
INPUTFORYEAR == CHOOSE($C$2, 'Inputs-Yearly'!$D$10, 'Inputs-Yearly'!$E$10, 'Inputs-Yearly'!$F$10, 'Inputs-Yearly'!$G$10, 'Inputs-Yearly'!$H$10, 'Inputs-Yearly'!$I$10)
So I wrote the following VBA code (mind you, I've never coded a single line of VBA code in my life before):
Public Function INPUTFORYEAR(model_input As Range, model_year As Number) As Range
' model_input would be the cell reference to a particular hard-coded input within the "Input-Yearly" tab that corresponds to a Year 1 input
' model_year would be 1, 2, 3, 4, 5, or 6
' INPUTFORYEAR would take in the two arguments above and output the hard-coded input not for Year 1, but for the model_year
Application.WorksheetFunction.Choose(model_year, Application.WorksheetFunction.Offset(model_input, 0, 1, 1, 1), Application.WorksheetFunction.Offset(model_input, 0, 2, 1, 1), Application.WorksheetFunction.Offset(model_input, 0, 3, 1, 1), Application.WorksheetFunction.Offset(model_input, 0, 4, 1, 1), Application.WorksheetFunction.Offset(model_input, 0, 5, 1, 1), Application.WorksheetFunction.Offset(model_input, 0, 6, 1, 1))
End Function
Two issues:
1. There are ~100 types of inputs (C2, C3, C4, C5, all the way to C100) and I'd have to do a Find+Replace 100x.
2. I've made a mistake somewhere in the VBA code and keep getting error messages.
SO,
*heaves a sigh* can anyone proffer a more elegant solution that would dynamically update input values a la the year and not have a huge 2-line formula every time I call the input value -- all within 1 tab?
Thank you thank you thank you!!!
Calantha
Bookmarks