+ Reply to Thread
Results 1 to 2 of 2

Formula array summing a dynamic range of columns

  1. #1
    Registered User
    Join Date
    02-04-2014
    Location
    Baltimore, Maryland
    MS-Off Ver
    Excel 2010
    Posts
    17

    Formula array summing a dynamic range of columns

    Based on a specified parameter given by the user, I need to create a formula array that will sum the total columns dictated by the users specified amount.Print screen.docx

    i.e. the user specifies the number of tasks, say x. Then my macro will generate x number of columns. I am then looking to have those summed up, but since the number of columns is dynamic, I don't know how to sum these in a formula array.

    I tried to use a for loop, but I don't know how that gets integrated into the code within the formula array code. I am new to programming and have found success in reverse engineering all the code after using the macro recorder, but I don't know the specific rules to dictate how to make this code work.

    This is the relevant code I have so far. M2 is where the user inputs the number of tasks. The code below only counts the kth row, but I need the formula to count from the kth row, to the kth-1 row, .... to 1.



    Dim RowCount As Integer
    RowCount = Selection.Rows.Count

    Dim i, j, k As Integer
    k = Range("M2").Value

    Range(Cells(11, k + 13), Cells(11 + RowCount - 1, k + 13)).Select
    Selection.FormulaArray = "=sum(RC[-" & k & "]:R[" & RowCount - 1 & "]C[-" & k & "],RC[-1]:R[" & RowCount - 1 & "]C[-1])"




    Is there a way for me to either use a for loop within this so that I can sum as follows:

    Selection.FormulaArray = "=sum(RC[-" & k & "]:R[" & RowCount - 1 & "]C[-" & k & "], ... ..., RC[-" & 1 & "]:R[" & RowCount - 1 & "]C[-" & 1 & "])"

    Since the number of columns is dynamic, I can't manually code each of the columns summing in the array.

    I know that somehow it has to be a for loop as follows:

    Dim a As Integer
    for a = k to 1

    ***SEE PRINT SCREEN ATTACHED FOR AN IDEA OF WHAT THE CODE IS TRYING TO DO

    Thanks for everyone's assistance in advance!!!!

  2. #2
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Formula array summing a dynamic range of columns

    You could give all the task cells on one row a name, let's say "Tasks".
    This name can then be referenced in vba with range("Tasks"). So when inserting or deleting tasks you can adjust the number of columns that range spans accordingly.
    Then you can use this formula to sum the number of tasks:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Make sure the name (or range) has references rows relatively like: $B6:$H6. This will sum the tasks in column B-H on row 6. And should be in cell I6 (according to your example).

    No need to construct or even use array formulas on the fly.

    BTW: if you need any additional help, please know that after next Sunday I will be away for the next 3 weeks and not able to answer your posts.
    Last edited by Tsjallie; 02-08-2014 at 05:23 PM.
    Cheers!
    Tsjallie




    --------
    If your problem is solved, pls mark the thread SOLVED (see Thread Tools in the menu above). Thank you!

    If you think design is an expensive waste of time, try doing without ...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Help Need Formula Array to sum dynamic range of columns VBA Macro
    By UMBiii in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 02-04-2014, 08:05 PM
  2. Dynamic range definition in array formula.
    By donjoe in forum Excel General
    Replies: 3
    Last Post: 12-22-2010, 02:05 PM
  3. VBA for dynamic range in array formula
    By oliver30680 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-01-2010, 07:31 PM
  4. array formula with a dynamic range.
    By Dave in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-26-2006, 01:25 AM
  5. Replies: 0
    Last Post: 08-28-2005, 10:52 AM

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