Consulting

Results 1 to 6 of 6

Thread: Solved: Loop To Populate Formula

  1. #1
    VBAX Regular
    Joined
    Jul 2007
    Posts
    30
    Location

    Solved: Loop To Populate Formula

    Hello

    I have a formula that takes to long to calculate for an entire workbook (crashs workbook).
    I though I could loop through selected cells copying the formula into the cell, calculating, copy and paste as values and move onto the next however I cannot seem to get it to work. Here is my attempt.

    Sub Insert_Formulas()

    Sheets("Detailed analysis").Select
    Dim C As Range

    'Loop format pivot table borders
    Range("H8:L32").Select

    For Each C In Selection.Cells
    ActiveCell.FormulaR1C1 = "=VLOOKUP(R5C,INDIRECT.EXT(""'""&RC5),3,FALSE)"
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Selection.Interior.ColorIndex = 15
    Next C
    End Sub

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Nothing obvious that I can see, but I have to ask why you precede R5 with an apostrophe in the value passed to INDIRECT.EXT.

    I assume you have installed that addin.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Jul 2007
    Posts
    30
    Location
    Yes that addin has been installed

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi,
    There is only one ActiveCell in the range, so the code writes to that cell repeatedly. Write the formula to C instead.
    [vba]
    Sub Insert_Formulas()
    Dim C As Range
    'Loop format pivot table borders
    Range("H8:L32").Select
    For Each C In Selection.Cells
    C.FormulaR1C1 = "=VLOOKUP(R5C,INDIRECT.EXT(""'""&RC5),3,FALSE)"
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Selection.Interior.ColorIndex = 15
    Next C
    End Sub
    [/vba]

    Alternatively, you don't need to loop.
    [vba]
    Sub Insert_Formulas2()
    With Range("H8:L32")
    .FormulaR1C1 = "=VLOOKUP(R5C,INDIRECT.EXT(""'""&RC5),3,FALSE)"
    .Copy
    .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    .Interior.ColorIndex = 15
    End With
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Doh! How did I miss that?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Regular
    Joined
    Jul 2007
    Posts
    30
    Location
    Sorry one more question is it possible to get this to work on multiple range, ie

    Range("G6:W79,Z6:AD79").Select

    Thanks
    Last edited by bradh_nz; 08-23-2007 at 07:17 AM.

Posting Permissions

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