Consulting

Results 1 to 8 of 8

Thread: Solved: Summing a dynamic range

  1. #1
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,101
    Location

    Solved: Summing a dynamic range

    I would like to enter a formula in cell D2 which sums up a dynamic range starting in column G2 and extending to the right. Is this the correct approach in line #9

    [vba]Private Sub cmdNewEntry_Click()
    Dim NewRow As Long, LastCol as Long
    With Worksheets("Results")
    NewRow = Cells(Rows.Count, "A").End(xlUp).Row + 1
    LastCol = Cells(Columns.Count,"G").End(xlRight).Column
    Cells(NewRow, 1).Formula = "=Row()-1"
    Cells(NewRow, 2).Value = Me.txtCaptain.Value
    Cells(NewRow, 3).Value = Me.txtPOB.Value
    Cells(NewRow, 4).Formula = WorksheetFunction.Sum(Range(Range("$G2"), Range("$G2").End(xlRight)))
    'Cells(NewRow, 5).Formula = Lookup header for each row in Fish_Table to allocate points.
    Cells(NewRow, 6).Formula = "=IF($E2>0,RANK(Table2[[#This Row],[Points]],[Points],1),"")"
    End With
    End Sub[/vba]
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,888
    [vba]Cells(NewRow, 4).Formula = WorksheetFunction.Sum(Range(Range("$G2"), Range("$G2").End(xlToRight)))[/vba]will work, only if there are no spaces within the range of cells you want to sum.

    It will not necessarily put it into D2, depending on what NewRow is.

    [vba]NewRow = Cells(Rows.Count, "A").End(xlUp).Row + 1 [/vba] will give you the row number of the first blank row in column A, where you use column A, rather than any other column, to decide that.

    I see that you're trying to do something similar with getting the last column.
    Which row do you want to use to determine the last column? At the moment you're using row 256 (xl2003) or row 16384 if using xl2007!
    Edit post posting: Perhaps you're wanting to use NewRow to determine the last column?:
    [vba]LastCol = Cells(NewRow,Columns.Count).End(xlToLeft).Column[/vba]
    End of Edit post posting.

    If you're working in the same way as finding the last row you need something like:
    [vba]LastCol = Cells(2,Columns.Count).End(xlToLeft).Column[/vba]where the 2 means your using what's in row to 2 determine the last column. If you want to use LastCol in your sum formula then

    [vba]Cells(NewRow, 4).Formula = WorksheetFunction.Sum(Range("G2"),Cells(2,LastCol)))[/vba] or perhaps
    [vba]Cells(NewRow, 4).Formula = WorksheetFunction.Sum(cells(NewRow,6),Cells(NewRow,LastCol)))[/vba] where the 6 represents column G
    (All untested)
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,101
    Location
    Quote Originally Posted by p45cal
    [vba]Cells(NewRow, 4).Formula = WorksheetFunction.Sum(Range(Range("$G2"), Range("$G2").End(xlToRight)))[/vba]will work, only if there are no spaces within the range of cells you want to sum.

    It will not necessarily put it into D2, depending on what NewRow is.
    Thank you for the correction to the line. There will be no spaces and yes I am starting this formula in cell D2 and then will fill down as my table grows.

    [vba]NewRow = Cells(Rows.Count, "A").End(xlUp).Row + 1 [/vba] will give you the row number of the first blank row in column A, where you use column A, rather than any other column, to decide that.
    Yes, I am using column A to determine the last filled row

    I see that you're trying to do something similar with getting the last column.
    Which row do you want to use to determine the last column? At the moment you're using row 256 (xl2003) or row 16384 if using xl2007!
    The columns from G onwards (Maximum possible 10 columns to be added from and including column G) are added by another piece of code supplied by Bob and works well. All rows in the table will have the same length.

    Edit post posting: Perhaps you're wanting to use NewRow to determine the last column?:
    [vba]LastCol = Cells(NewRow,Columns.Count).End(xlToLeft).Column[/vba]
    End of Edit post posting.

    Up till this post I wasn't.

    If you're working in the same way as finding the last row you need something like:
    [vba]LastCol = Cells(2,Columns.Count).End(xlToLeft).Column[/vba]where the 2 means your using what's in row to 2 determine the last column. If you want to use LastCol in your sum formula then

    [vba]Cells(NewRow, 4).Formula = WorksheetFunction.Sum(Range("G2"),Cells(2,LastCol)))[/vba]
    or perhaps
    [vba]Cells(NewRow, 4).Formula = WorksheetFunction.Sum(cells(NewRow,6),Cells(NewRow,LastCol)))[/vba] where the 6 represents column G
    (All untested)
    The LastCol bit I forgot to take out. I had initially intended to use it to do something else and still may. I thank you for the hints.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    As you are using WITH, I would qualify the ranges to prevent possible errors if the macro is run from another sheet
    [VBA]
    With Worksheets("Results")
    NewRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
    LastCol = .Cells(Columns.Count,"G").End(xlRight).Column
    .Cells(NewRow, 1).Formula = "=Row()-1"
    .Cells(NewRow, 2).Value = Me.txtCaptain.Value
    .Cells(NewRow, 3).Value = Me.txtPOB.Value
    .Cells(NewRow, 4).Formula = WorksheetFunction.Sum(Range(.Range("$G2"), .Range("$G2").End(xlRight)))
    'etc.

    [/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
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,888
    True enough. I wonder whether Aussiebear realises that despite using .Formula (.Value is used elsewhere) he is still going to get a value and not a formula in that cell, which won't update as other cells change.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,101
    Location
    @ p45cal: I am wanting the value for the cells.

    Some of the values needed, will be generated by the use of a formula/s ,whilst other values will come from a form. The form will be called from a button on the Results worksheet and will be used only on the Results worksheet.

    @ mdmackillop: Can you qualify your comment further please?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    [vba]With Worksheets("Results")
    NewRow = .Cells(1,1).value
    end with
    [/vba] will return the value in Results cell A1


    [vba]With Worksheets("Results")
    NewRow = Cells(1,1).value
    end with
    [/vba] will return the value of A1 on the active sheet.
    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'

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,101
    Location
    Thanks Malcolm
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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