+ Reply to Thread
Results 1 to 6 of 6

Thread: Recording a macro with 12 or more nesting formulas. Excel 2007

  1. #1
    Registered User
    Join Date
    08-29-2010
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    2

    Recording a macro with 12 or more nesting formulas. Excel 2007

    Hi there,
    I am currently trying to record a macro that will help me bucketize marketing data that I work on. Even though most of the other nesting issues work correctly. I have trouble with one.
    as the macro record the nesting function in more than 1 line, I am assuming that the code is broken, hence there is a syntax error.
    Attached is the code.

    Application.CutCopyMode = False
    Columns("F:F").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("F2").Select
    ActiveCell.FormulaR1C1 = _
    "=IF(OR(RC5=""Less than 1 Million"",RC5=""1-3 Million"",RC5=""3-5 Million"",RC5=""5-10 Million""),""Under 10 Million"",IF(RC5=""10-25 Million"",""10-25 Million"",IF(RC5=""25-50 Million"",""25-50 Million"",IF(RC5=""50-100 Million"",""50-100 Million"",IF(RC5=""100-250 Million"",""100-250 Million"",IF(RC5=""250-500 Million"",""250-500 Million"",IF(RC5=""500 Million-1 Billion"",""500 Million-1 Billion"",IF(OR(RC5=""Over 25 Billion"",RC5=""1-10 Billion"",RC5=""11-25 Billion""),""Over 1 Billion"",IF(OR(RC5=""Confidential"",RC5="" ""),""Confidential"",""NA"")))))))))"
    Columns("J:J").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("I2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.NumberFormat = "0"
    Range("J2").Select
    ActiveCell.FormulaR1C1 = _
    "=IF(AND(RC9>=1,RC9<=25),""Fewer than 25"",IF(AND(RC9>=26,RC9<=50),""26-50"",IF(AND(RC9>=51,RC9<=100),""51-100"",IF(AND(RC9>=101,RC9<=500),""101-500"",IF(AND(RC9>=501,RC9<=1000),""501-1000"",IF(RC9>=1000,""Over 1,000"",IF(RC9<1,""Confidential"",""NA"")))))))"
    Range("J2").Select
    Selection.AutoFill Destination:=Range("J2:J152"), Type:=xlFillDefault
    Range("J2:J152").Select
    Columns("U:U").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("U2").Select
    ActiveCell.FormulaR1C1 = _
    "=IF(OR(RC20=""Business Services"",RC20=""Financial Services"",RC20=""Insurance"",RC20=""Banking"",RC20=""Media"",RC20=""Language Services"",RC20=""Consumer Services"",RC20=""Retail "",RC20=""Real Estate"",RC20=""Telecommunications Services""),""Business Services"",IF(OR(RC20=""Industrial Manufacturing"",RC20=""Chemicals"",RC20=""Metals & Mining"",RC20=""Industrial Manufacturing"",RC20=""Optics & Photonics"",RC20=""Security Products & Services""),""Industrial Manufacturing"",IF(RC20=""Aerospace & Defense"",""Aerospace & Defense"",IF(OR(RC20=""Food and Drink"",RC20=""Agriculture""),""Food and Drink"",IF(OR(RC20=""Automotive & Transport"",RC20=""Transportation Services""),""Automotive & Transport"",IF(OR(RC20=""Biotech & Pharma"",RC20=""Health Care""),""Biotechnology"",IF(OR(RC20=""Energy & Utilities"",RC20=""Waste Management Services""),""Alternative energy"",IF(OR(RC20=""Computer Software"",RC20=""Computer Hardware"",RC20=""Computer Services"",RC20=""Electronics""),""Information Technology"",IF(OR(RC
    20=""Consumer Product Manufacturing"",RC20=""Security Products & Services"",RC20=""Telecommunications Equipment""),""Consumer Product Manufacturing"",IF(OR(RC20=""Associations and Government"",RC20=""Membership Organizations"",RC20=""Foundations"",RC20=""Government""),""Associations and Government"",IF(RC20=""Construction"",""Construction"",IF(RC20="""",""Empty Field"",""NA""))))))))))))"
    Range("U1").Select
    ActiveCell.FormulaR1C1 = "Industry class"
    With ActiveCell.Characters(Start:=1, Length:=14).Font
    .Name = "Arial"
    .FontStyle = "Bold"
    .Size = 8
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .Color = -16777216
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
    End With
    Range("U2").Select
    Selection.AutoFill Destination:=Range("U2:U152"), Type:=xlFillDefault
    Range("U2:U152").Select
    End Sub

    Now I tried line break with " _" and then proceeded the next line with " &" but I still have challenges. Any help would be appreciated.

    Thanks
    Last edited by arjun_mail; 08-29-2010 at 09:33 PM. Reason: adding code tag

  2. #2
    Valued Forum Contributor Norie's Avatar
    Join Date
    02-02-2005
    Posts
    512

    Re: Recording a macro with 12 or more nesting formulas. Excel 2007

    Why not 'fix' the formula?

    Almost any formula with that many nested IFs can be restructured to be at least a bit shorter and perhaps even more efficient.

    As for the macro recorder, it's a great tool but you shouldn't rely on it coming up with the exact code you need.

    Use it to get the syntax you need and change it for your ultimate enquiries.

    I'm no formula guru but taking a quick look at them I can see that the 2nd one at least can be shortened, probably using some sort of lookup.

    Here's what I came up with for the formula in J2.

    =IF(I2<1, "Confidential", LOOKUP(I2, {0,26,51,101,501,1001},{"Fewer than 25","26-50","51-100","101-500","501-1000","Over 1,000"}))

    I know it's missing a bit but you should get the idea, for that formula at least.

  3. #3
    Registered User
    Join Date
    08-29-2010
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Recording a macro with 12 or more nesting formulas. Excel 2007

    thanks for the help, I guess your right about making it shorter with the first 2 functions as this will not make the excel file 2 heavy. However with the last part, I am trying to funnel all the options into 10-15 to make it look more proportionate on the graph.

  4. #4
    Forum Guru
    Join Date
    10-28-2008
    Location
    Not here anymore
    MS-Off Ver
    irrelevant
    Posts
    10,151

    Re: Recording a macro with 12 or more nesting formulas. Excel 2007

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

  5. #5
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Recording a macro with 12 or more nesting formulas. Excel 2007

    Please change your QUOTE tags to CODE tags.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,298

    Re: Recording a macro with 12 or more nesting formulas. Excel 2007

    arjun -
    You need to click on the Green Edit button below your message, go to Advanced and highlight/sekect your code and click on the # icon above the text. This will put Code tags around the selected text. Then save the changes.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0