+ Reply to Thread
Results 1 to 5 of 5

Sum two rows in a range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Sum two rows in a range

    Hi. I have a "big" range and a smaller one in which each cell is the sum of two cells in the big range. It's more simple in the file attached. I was thinking about something like this, any modification/other suggestion?

    Private Sub test()
    Dim a As Range, b As Range, c As Byte
        With Worksheets("s")
        Set a = .Range("a2:e6")
            For Each b In a
            b.Value = Application.WorksheetFunction.Sum(.Cells(b.Offset(9), b.Offset(11)))
            Next b
        End With
    End Sub
    Attached Files Attached Files
    Last edited by GIS2013; 07-05-2014 at 08:23 AM. Reason: Now with the right sample file
    Please * if you like the answer

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Sum two rows in a range

    Hi, v,

    you should look up the VBA help for Cells which requires the Row number first and either a numeric value or alphabetic string for the Column.

    You would need to adapt the sheet name as the name in the attachment differs from s:
    Private Sub test()
    Dim rngCell As Range
    For Each rngCell In Worksheets("s").Range("A2:E6")
      With rngCell
        .Value = WorksheetFunction.Sum(.Offset(9), .Offset(11))
      End With
    Next rngCell
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Sum two rows in a range

    Hi Holger,
    Thank you, I'll read again about CELLS.
    I could better exmplain myself from the first place: the "big" range I was relating to is on a monthly basis, and what I'm trying to do is to create a new range which will be based on the "big" range but be on a quarter basis.
    For example if A10 = X, A11 = Y and A12=Z then A1, which represents a quarter, will be equal to X+Y+Z.
    Hope it's more clear now.
    Attached Files Attached Files

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Sum two rows in a range

    Hi, GIS2013,

    could you please make clear if you want to add two rows like indicated in the title or 3 rows like your last post which would make up a quarter? In the latter part for the second row it should be 13 to 15?

    Private Sub test()
    Dim rngCell As Range
    For Each rngCell In Worksheets("1").Range("A2:E6")
      With rngCell
        .Value = WorksheetFunction.Sum(.Offset(8 + (.Row - 2) * 2).Resize(3, 1))
      End With
    Next rngCell
    End Sub
    Ciao,
    Holger

  5. #5
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Sum three rows in a range into a new range

    I'm sorry for the confusion. Yes - quarter, 13:15, and in the third row 16:18 etc. I've just run your code and it works smoothly. I'll also take time to learn about RESIZE. Thanks Holger

+ 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. [SOLVED] First unhide all rows - then hide rows based on specific cell value for a range of cells
    By robbiekh in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-22-2013, 05:46 PM
  2. Replies: 4
    Last Post: 09-24-2013, 05:44 AM
  3. excel 2007 macro need to have variable range of rows rather than fixed range
    By JW1028 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-20-2013, 02:45 PM
  4. Hide rows in range 2 based on filter selection in range 1
    By gtol in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-28-2012, 02:09 PM
  5. Replies: 1
    Last Post: 06-17-2006, 09:10 PM

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