Macro to sum selected cells in a row and repeat for all rows in a column with values

KlayontKress

Board Regular
Joined
Jan 20, 2016
Messages
67
Office Version
  1. 2016
Platform
  1. Windows
To all,


I'm looking for a macro that will allow the user to select multiple cells (up to 5) in a row (Lets say E10 and G10) and then add the sum of those two cells into the left most cell selected (this will usually be the first cell selected but it's possible the user will select the calues in a different order than left to right (we can standardize if we need to)).

I then need the macro to go through and repeat this for every row in a column with values use the same relative positions as the first series of values selected.


For example, from the chart below, lets say I select D4, E4,G4,M4. I want to placed the summed value of these into cell D4 (not the formula for the sum but the actual value). In this case it would be 23. I then need the macro to remember that the left most column is "D" and that the initial values selected were in relative positions of +0, 1, 3, and 9 to go through and do this same calculation for every row in column D with a value and put in the summed value. The macro would then deleted all of the columns for the values selected except for the left most one where the summed values went.


The values in column "D" would end up being:

D4=23
D6=27
D8=31
D10=35
D12=39

and columns E,G, and M in the column below would be deleted.

ABCDEFGHIJKLMNO
1
2groupstyleRoom 1Room 2Room 3Room 4Room 5Room 6Room 7Room 8Room 9Room 10
3
4baseblah10123456789
5
6level 1blah blah112345678910
7
8level 2blah blah blah1234567891011
9
10level 3blah blah blah blah13456789101112
11
12level 4blaah145678910111213

<tbody>
</tbody>


Thanks for your help,
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
this should get you started. Only thing it does not do is delete the columns. I have a meeting so cant work on that.

Code:
Sub FORS()

Dim wb As Workbook
Dim ws As Worksheet
Dim lngROW As Long, lngCOL As Long
Dim cell1 As Range, cell2 As Range, cell3 As Range, cell4 As Range, cell5 As Range
Dim int1 As Integer, int2 As Integer, int3 As Integer, int4 As Integer, int5 As Integer, intST As Integer, intROW As Integer
Dim rng As Range, cell As Range
Dim dSUM As Double
Dim i As Variant

Set wb = ThisWorkbook
On Error Resume Next
Set cell1 = Application.InputBox(Prompt:="Please select a cell", Title:="cell selection", Type:=8)
On Error GoTo 0
If cell1 Is Nothing Then
    MsgBox "User failed to select a cell.  Please start again"
    End
End If
int1 = cell1.Column
intROW = cell1.Row
Set ws = ActiveSheet

lngCOL = ws.Cells.Find(What:="*", _
        after:=ws.Cells(1), _
        LookAt:=xlPart, _
        LookIn:=xlFormulas, _
        SearchOrder:=xlByColumns, _
        SearchDirection:=xlPrevious, _
        MatchCase:=False).Column





On Error Resume Next
Set cell2 = Application.InputBox(Prompt:="Please select a cell", Title:="cell selection", Type:=8)
On Error GoTo 0
If cell2 Is Nothing Then
    MsgBox "User failed to select a cell.  Please start again"
    End
End If
If cell2.Row <> intROW Then
    MsgBox "User selected a cell on different row.  Please start again"
    End
Else
    int2 = cell2.Column
End If
On Error Resume Next
Set cell3 = Application.InputBox(Prompt:="Please select a cell", Title:="cell selection", Type:=8)
On Error GoTo 0
If cell3 Is Nothing Then
    MsgBox "User failed to select a cell.  Please start again"
    End
End If
If cell3.Row <> intROW Then
    MsgBox "User selected a cell on different row.  Please start again"
    End
Else
    int3 = cell3.Column
End If
On Error Resume Next
Set cell4 = Application.InputBox(Prompt:="Please select a cell", Title:="cell selection", Type:=8)
On Error GoTo 0
If cell4 Is Nothing Then
    MsgBox "User failed to select a cell.  Please start again"
    End
End If
If cell4.Row <> intROW Then
    MsgBox "User selected a cell on different row.  Please start again"
    End
Else
    int4 = cell4.Column
End If
On Error Resume Next
Set cell5 = Application.InputBox(Prompt:="Please select a cell", Title:="cell selection", Type:=8)
On Error GoTo 0
If cell5 Is Nothing Then
    MsgBox "User failed to select a cell.  Please start again"
    End
End If
If cell5.Row <> intROW Then
    MsgBox "User selected a cell on different row.  Please start again"
    End
Else
    int5 = cell5.Column
End If

intST = WorksheetFunction.Min(int1, int2, int3, int4, int5)

dSUM = cell1 + cell2 + cell3 + cell4 + cell5
ws.Cells(intROW, intST).Value = dSUM

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,466
Members
449,086
Latest member
kwindels

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