I am working on a project that is basically a calculation tool. I have two drop down lists that I'm pulling info from, and need to do a specific calculation based on the selection between the two drop down lists.
Basic Scenario is if cell D13 equals "something" and H13 equals "Something" then do some math, but if D13 equals "something else" and H13 equals "Something else" then do different math.
My starting code is as follows (which works)
Range("D20").Select
Selection.Formula = "=IF(AND(D13=""something"",H13=""something""),(D15*H15)*Sheet2!B2+D17*Sheet2!H2+H17*Sheet2!H3+K17*Sheet2!H4,IF(AND(D13=""something"",H13=""somethingelse""),(D15*H15)*Sheet2!C2+D17*Sheet2!H2+H17*Sheet2!H3+K17*Sheet2!H4,IF(AND(D13=""somethingelse"",H13=""something""),(D15*H15)*Sheet2!D2+D17*Sheet2!H2+H17*Sheet2!H3+K17*Sheet2!H4,IF(AND(D13=""Somethingelse"",H13=""somethingelse""),(D15*H15)*Sheet2!E2+D17*Sheet2!H2+H17*Sheet2!H3+K17*Sheet2!H4,""""))))"
I tried entering line breaks as follows, but keep getting error messages no matter where I try putting the underscore:
Range("D20").Select
Selection.Formula = "=IF(AND(D13=""something"",H13=""something""),(D15*H15)*Sheet2!B2+D17*Sheet2!H2+H17*Sheet2!H3+K17*Sheet2!H4, _
IF(AND(D13=""something"",H13=""somethingelse""),(D15*H15)*Sheet2!C2+D17*Sheet2!H2+H17*Sheet2!H3+K17*Sheet2!H4, _
IF(AND(D13=""somethingelse"",H13=""something""),(D15*H15)*Sheet2!D2+D17*Sheet2!H2+H17*Sheet2!H3+K17*Sheet2!H4, _
IF(AND(D13=""Somethingelse"",H13=""somethingelse""),(D15*H15)*Sheet2!E2+D17*Sheet2!H2+H17*Sheet2!H3+K17*Sheet2!H4,""""))))"
The error usually points to the second "IF" saying "Compile Error: Expected: end of statement"
I know this code is long and ugly, and I need to clone it several more times for the worksheet including &IF scenarios, so I certainly am willing to simplify it if possible. I am not much of a coder or programmer, and have been relying on the Excel Macro Recorder to generate my VBA codes.
Thanks in advance for your patience with me and your help.
Bookmarks