Consulting

Results 1 to 7 of 7

Thread: Solved: SUM Formula

  1. #1
    VBAX Regular
    Joined
    Oct 2010
    Posts
    49
    Location

    Solved: SUM Formula

    Hi Guys,

    Could someone please let me know how to write the formula to SUM two ranges when the two ranges have been defined as variables of type range?

    E.g
    [VBA]
    Dim rangeOne as Range
    Dim rangeTwo as Range

    Set rangeOne = Range("A1")
    Set rangeTwo = Range("A5")
    [/VBA]

    So if I wanted to use "rangeOne" and "rangeTwo" in the SUM formula, how would that be written?

    [VBA]
    Range("B1").Formula = ????
    [/VBA]

    Thank you!

    Kind Regards,

    Giri

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

    Range("B1").Formula = "=" & rangeone.address & "+" & rangetwo.address[/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
    Oct 2010
    Posts
    49
    Location
    Oops Sorry... should have been more clear.

    I meant, summing "A1:A5".

    Thanks xld.

    Kind Regards,

    Giri

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Range ("B1").Formula = "=Sum" & Range0ne.Address & "+" & Rangetwo.Address
    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

  5. #5
    VBAX Regular
    Joined
    Oct 2010
    Posts
    49
    Location
    Thanks Aussie Bear.

    For some reason I am getting "Run Time Error 1004: Application Defined Error"

    This is the code I have. I'm not sure what I am doing wrong because all the variables seem to be referenced correctly.

    [VBA]
    Giri.Offset(-2, 0).Formula = "=SUM" & topRed.Address & "+" & redEnd.Address
    [/VBA]

    Any ideas what RunTime Error 1004 refers to?

    Thanks.

    Kind Regards,

    Giri

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

    Dim rangeOne As Range
    Dim rangeTwo As Range

    Set rangeOne = Range("A1")
    Set rangeTwo = Range("A5")

    Range("B1").Formula = "=SUM(" & Range(rangeOne, rangeTwo).Address & ")"
    [/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

  7. #7
    VBAX Regular
    Joined
    Oct 2010
    Posts
    49
    Location
    Perfect, xld!

    Thank you!

    Kind Regards,

    Giri

Posting Permissions

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