Consulting

Results 1 to 4 of 4

Thread: Solved: Sum Dynamic row in VBA

  1. #1
    VBAX Regular
    Joined
    Aug 2005
    Posts
    18
    Location

    Solved: Sum Dynamic row in VBA

    Hi have being trying to search the threads but unable to find any close to what i am trying to do. Am still trying to improve on VBA.

    [VBA]
    Sub sum_rows()
    Dim no_of_rows as variant

    no_of_rows=range("a5") 'A5 contains the value of nos of rows of data to sum. This value is variable. eg if A5=12

    Range("G20").select 'this the cell selected to input the Sum formula
    ActiveCell.FormulaR1C1 = "=SUM(r[-no_of_rows]C:R[-1]C)"

    'The above formula is supposed to sum the nos of rows up from G8 to G19 since A5=12

    end sub
    [/VBA]
    However when i execute this, i keep getting "Application-defined or Object defined error".
    Thanks for any help rendered. Murf fm Singapore

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub sum_rows()
    Dim no_of_rows As Variant

    no_of_rows = Range("A5").Value
    Range("G20").FormulaR1C1 = "=SUM(r[-" & no_of_rows & "]C:R[-1]C)"

    End Sub
    [/vba]
    ____________________________________________
    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
    Aug 2005
    Posts
    18
    Location
    wow, thanks thanks thanks, solve!
    just wondering where do you find when to add in the ampersand symbol?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It's obvious if you know the rules. A string is set of characters between two quotes. If you want to include a variable value you have to close the string and concatenate (&) the variable value then reopen the string.
    ____________________________________________
    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

Posting Permissions

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