none
How to add back a number to itself throughout a range. RRS feed

  • Question

  • Hi,

    I am a new to VBA. So please excuse if i am not conveying my thought properly.

    I want to add a range of numbers to another range of numbers. However second range should always add to itself with first corresponding array values.  For example my firsr array is A1:F10 and second array is A15:F25. Now i want A15 = A15+A1, A16= A16+A2, A17= A17+A3 and so on. Every time second array should retain itself value and get added with first corresponding array values.

    Thanks in advance for your kind help on this. I hope someone from expert will make my job easy. Looking forward for kind help!

    Saturday, August 2, 2014 10:59 AM

All replies

  • Re:  add a number to itself

     

    An equation in VBA is evaluated first on the right side of the equal sign.
    If x has a value of 3 then...
    x = x + 1 'equals 4
    '---
    Sub AddThemUp()
      Dim i As Long
      Dim j As Long
      Dim rng1 As Range
      Dim rng2 As Range

      Set rng1 = Range("A1:F10")
      Set rng2 = Range("A15:F24")  '<<< Note
      For i = 1 To rng1.Rows.Count
        For j = 1 To rng1.Columns.Count
         rng2(i, j).Value2 = rng1(i, j) + rng2(i, j).Value2
        Next
      Next
      'Range("H2").Value2 = Application.WorksheetFunction.Sum(Range("A15:F24"))
    End Sub
    '---

    Jim Cone
    Portland, Oregon USA
    https://goo.gl/IUQUN2 (Dropbox)
    (free & commercial excel add-ins & workbooks)

    • Edited by James Cone Sunday, October 30, 2016 7:56 PM
    Saturday, August 2, 2014 1:20 PM