+ Reply to Thread
Results 1 to 7 of 7

how to paste an array of formulas into worksheet via VB

Hybrid View

  1. #1
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    how to paste an array of formulas into worksheet via VB

    Right now I have code that pastes a few dozen formulas one by one into a worksheet. Some of the formulas are in cells that are adjacent to one another.

    So for examle, cell A1's formula that VB pastes in is =B24*3+1, cell B1's formula is =B24*5+6, cell C1's formula is =B14*5+12, and so on.... The precess of pasting one by one is a bit time consuming. Is there a way to define 10 formulas or so into an array of sorts in VB and paste it in in one step in all 10 horizontally adjacent cells vs. pasting 10 formulas one buy one in 10 steps?

    Thank you.
    Last edited by luv2glyd; 09-29-2010 at 08:42 AM.
    You either quit or become really good at it. There are no other choices.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: how to paste an array of formulas into worksheet via VB

    If the formulas are distinctive, listing them out in separate lines of code makes maintenance of your macro and future editing simple. I would recommend leaving the individual formulas coded as you have.

    This is based on your description, if you had posted your actual code we would be able to tell for certain if this is best.

    Be sure to turn off ScreenUpdating at the top of your macro, and turn it back on at the end, this will speed up the execution of all your macros significantly.

    Sub MyMacro()
    Application.ScreenUpdating = False
    
    'your code
    
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Re: how to paste an array of formulas into worksheet via VB

    Great! That speeds things up a lot! Although I'd still like to know if and how the array pasting can be done for future refence. Here's the code I have:
        Range("F33").Formula = "=INDEX($daily_signs_array,1)"
        Range("G33").Formula = "=INDEX($daily_signs_array,2)"
        Range("H33").Formula = "=INDEX($daily_signs_array,3)"
        Range("I33").Formula = "=INDEX($daily_signs_array,4)"
        Range("J33").Formula = "=INDEX($daily_signs_array,5)"
        Range("K33").Formula = "=INDEX($daily_signs_array,6)"
        Range("L33").Formula = "=INDEX($daily_signs_array,7)"
        Range("M33").Formula = "=INDEX($daily_signs_array,8)"
        Range("N33").Formula = "=INDEX($daily_signs_array,9)"
        Range("O33").Formula = "=INDEX($daily_signs_array,10)"
        Range("P33").Formula = "=INDEX($daily_signs_array,11)"
        Range("Q33").Formula = "=INDEX($daily_signs_array,12)"
        Range("R33").Formula = "=INDEX($daily_signs_array,13)"
        Range("S33").Formula = "=INDEX($daily_signs_array,14)"

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: how to paste an array of formulas into worksheet via VB

    I get the same result (after taking out the $ sign, not sure how you're able to use that and I'm not) with this one line of code:
    Sub EnterArray()
        Range("F33:S33").Formula = "=INDEX(daily_signs_array,COLUMN(A1))"
    End Sub

  5. #5
    Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    526

    Re: how to paste an array of formulas into worksheet via VB

    Along with suppressing screen updating, hope this also would speed up the process

    
    Application.Calculation = xlCalculationManual
    
    'your codes
    
    Application.Calculation = xlCalculationAutomatic
    rgds

    johnjohns

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: how to paste an array of formulas into worksheet via VB

    or

    [A1:C1]=split("=B24*3+1|=B24*5+6|=B14*5+12","|")
    [A1:C1] = [A1:C1].Formula



  7. #7
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Re: how to paste an array of formulas into worksheet via VB

    Perfect! That simplifies my code a lot and gives me some versatility in the future. Application.Calculation also helps speed things up. Thank you everyone.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1