VBA sum pairs of cells from dynamic input list

Probe

New Member
Joined
Sep 8, 2024
Messages
4
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Dear Experts, I am trying to generate a code to sum pairs in the input block. The input data B5:B11 thru to F5:F11 is dynamic with some blank cells before (B1:B4) - after the input block, B12:B13. I manually generated a computed block B13:B34 to F14:F34 right under the input block for clarity, though it can be computed block can be plotted anywhere in the same sheet or copied to another sheet. The problem I have is that dynamic input block B5:B11 F5:F11 can be a lot longer than 7 raws, therefore without vba it requires manipulations with formula everytime when block gets longer. Would appreciate your inputs how to tackle this challenge
B14 = B5+B6,
B15 = B5+B7,
B16= B5 + B8
...
B20= B6 + B7
B21 = B6 + B8
...
B25 = B7 + B8
B26 = B7 + B9
...

1725866277377.png
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Something like this perhaps? Changing the value of startRow and startCol will change where the output goes to.

VBA Code:
Sub Probe()
    Dim numBlock As Variant
    Dim i As Long, j As Long, k As Long
    Dim cRow As Long, startRow As Long, startCol As Long
    startRow = 14
    startCol = 9
    cRow = startRow
    numBlock = Range("B5").CurrentRegion
    k = 0
    Do While k < UBound(numBlock, 2)
        k = k + 1
           For i = LBound(numBlock, 1) To UBound(numBlock, 1) - 1
            For j = i + 1 To UBound(numBlock, 1)
                    Cells(cRow, startCol) = numBlock(i, k) + numBlock(j, k)
                    cRow = cRow + 1
            Next j
       Next i
       startCol = startCol + 1
       cRow = startRow
    Loop
End Sub
 
Upvote 0
Something like this perhaps? Changing the value of startRow and startCol will change where the output goes to.

VBA Code:
Sub Probe()
    Dim numBlock As Variant
    Dim i As Long, j As Long, k As Long
    Dim cRow As Long, startRow As Long, startCol As Long
    startRow = 14
    startCol = 9
    cRow = startRow
    numBlock = Range("B5").CurrentRegion
    k = 0
    Do While k < UBound(numBlock, 2)
        k = k + 1
           For i = LBound(numBlock, 1) To UBound(numBlock, 1) - 1
            For j = i + 1 To UBound(numBlock, 1)
                    Cells(cRow, startCol) = numBlock(i, k) + numBlock(j, k)
                    cRow = cRow + 1
            Next j
       Next i
       startCol = startCol + 1
       cRow = startRow
    Loop
End Sub
THIS IS GOLD! appreciate your time, myall_blues
 
Upvote 0

Forum statistics

Threads
1,221,417
Messages
6,159,789
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top