How to Sum a Range of Cells in a Row Using Excel VBA (6 Methods)

To demonstrate our methods, we have a dataset with three columns: “Fruit,” “March Sale,” and “April Sale.”

excel vba sum range of cells in row


Method 1 – Using the FormulaR1C1 Property to Sum Range of Cells

Steps:

  • Press ALT + F11 to bring up the VBA Window.

Alternatively, you can do this from the Developer tab >>> select Visual Basic.

  • From Insert >>> select Module.

This will show the VBA Module window. We’ll type our code here.

excel vba sum range of cells in row

  • Enter the following code in the VBA Module:
Sub SumInRow()
Dim x, rTotal As Range
Set x = Range("B5:D" & Range("B" & Rows.Count).End(xlUp).Row)
Set rTotal = x.Offset(, 3).Resize(x.Rows.Count, 1)
With rTotal
    .FormulaR1C1 = "=SUM(RC[-2]:RC[-1])"
    .Value = .Value
End With
End Sub

VBA Code Breakdown

  • Firstly, we’re calling our Sub Procedure SumInRow.
  • Secondly, we declare our variable types.
  • Thirdly, we’re setting our cell range for sum operation.
  • Finally, with the FormulaR1C1 property, we’re adding the row totals.
  • Here, RC[-2] means 2 columns left, and RC[-1] means 1 column left.
  • We’re telling E5 = C5 + D5 here.

  • Save the code.
  • Click anywhere inside the code.
  • Press the Run button.

excel vba sum range of cells in row

This will execute our code, and the added values will be seen in column E.


Method 2 – Using the VBA Evaluate Function to Sum a Range of Cells

Steps:

Sub SumInRowEvaluate()
Dim x As Range
Set x = Range("C5:D10")
For y = 1 To x.Rows.Count
    x.Rows(y).Cells(1, 3).Value = Evaluate("SUM(" & x.Rows(y).Address & ")")
Next y
End Sub

VBA Code Breakdown

  • Firstly, we’re calling our Sub Procedure SumInRowEvaluate.
  • Secondly, declaring our variable types.
  • Thirdly, we’re setting our cell range for sum operation.
  • Finally, with a For Next Loop, we’re adding the row totals.

excel vba sum range of cells in row

  • Save the Module and press the Run button.

Note: This time, we didn’t click inside our code; hence, the Macros dialog box will appear.

  • Select “SumInRowEvaluate” and press Run.

excel vba sum range of cells in row

We’ll use VBA to sum the range of cells in rows.


Method 3 – Using the Dynamic VBA Code to Find the Output Location to Place a Sum Automatically

Steps:

Sub SumInRowDynamic()
Dim x As Long, y As Long
x = Rows(4).Find("Total Sale", , , 1).Column - 2
y = Cells(Rows.Count, 2).End(xlUp).Row
    With Range(Cells(5, x + 2), Cells(y, x + 2))
        .Formula = "=SUM(RC[-" & x & "]:RC[-1])"
        .Value = .Value
    End With
End Sub

VBA Code Breakdown

  • Firstly, we’re calling our Sub Procedure SumInRowDynamic.
  • Secondly, we declare our variable types.
  • Thirdly, we’re searching for our output column name “Total Sale” in the dataset using the VBA Find method.
  • Then, we set our cell range for the sum operation.
  • Finally, we’re finding the row totals inside the With Statement.

excel vba sum range of cells in row

  • Save the code.
  • Click anywhere inside the code.
  • Press the Run button.

This will execute our code, and we’ll see the added values in E5:E10. This is what the final step should look like.

excel vba sum range of cells in row


Method 4 – Summing Rows Based on the Dynamic Range of Columns and Display the Formula

Steps:

Sub SumRowShowFormula()
    Dim x As Long
    For x = 5 To Cells(Rows.Count, "B").End(xlUp).Row
        Cells(x, "E").Formula = Replace("=sum(C@:D@)", "@", x)
    Next
End Sub

VBA Code Breakdown

  • Firstly, we’re calling our Sub Procedure SumRowShowFormula.
  • Secondly, we declare our variable types.
  • Thirdly, the numbers in our data start from row 5. Hence, we’ve put 5 in our code.
  • Then, we find the last position of our row using the Range End property.
  • After that, the VBA Sum function will add our values.
  • Finally, we’re using the Range Replace method to show our formula in cells.

  • Save the code.
  • Click anywhere inside the code.
  • Press the Run button.

excel vba sum range of cells in row

This will execute our code, and we’ll see the added values in E5:E10. We can see the SUM function if we select any cells from the output.


Method 5 – Inserting the VBA WorksheetFunction.Sum

Steps:

Sub SumRowWFunction()
    Dim x As Long
    For x = 5 To Cells(Rows.Count, "B").End(xlUp).Row
        Cells(x, "E") = WorksheetFunction.Sum(Range("C" & x & ":D" & x))
    Next
End Sub

VBA Code Breakdown

  • Firstly, we’re calling our Sub Procedure SumRowWFunction.
  • Secondly, we declare our variable types.
  • Thirdly, the numbers in our data start from row 5. Hence, we’ve put 5 in our code.
  • Then, we find the last position of our row using the Range End property.
  • After that, the Worksheet Sum function will add our values.

excel vba sum range of cells in row

  • Save and close the Module.
  • From the Developer tab >>> select Macros.

The Macro dialog box will appear.

  • Select “SumRowWFunction” from the list.
  • Press the Run button.

We’ve shown yet another code to add a range of cells in the row.

excel vba sum range of cells in row


Method 6 – Finding the Range of Cells to Sum in a Row

Steps:

Sub SumInRowsDynamicRange2()
Dim wSheet As Worksheet
Dim x As Long
Dim y As Long
Application.ScreenUpdating = False
Set wSheet = Sheets("dynamic2")
x = wSheet.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
y = wSheet.Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
wSheet.Cells(5, y).Resize(x - 4).FormulaR1C1 = "=SUM(RC3:RC[-1])"
Application.ScreenUpdating = True
End Sub

VBA Code Breakdown

  • Firstly, we’re calling our Sub Procedure SumInRowsDynamicRange2.
  • Secondly, declaring our variable types.
  • Thirdly, we’re setting our Sheet name “dynamic2”.
  • After that, we found our dataset range.
  • Finally, we’re adding the values in rows.

  • Save the Module and Run it.

We’ve achieved our goal of a sum range of cells in the row.

excel vba sum range of cells in row


Practice Section

We’ve provided practice datasets in the Excel file.


Download Practice Workbook

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Rafiul Haq
Rafiul Haq

Rafiul Haq worked as an Excel and VBA Content Developer in Exceldemy for over two years and published almost 200 articles for the website. He is passionate about exploring new aspects of Excel and VBA. He received his Bachelor of Science in Mechanical and Production Engineering (MPE) from the Islamic University of Technology. Rafiul furthered his education by obtaining an MBA in Finance from the Institute of Business Administration (IBA) at the University of Dhaka. Apart from creating... Read Full Bio

2 Comments
  1. I understand the VBA code to sum a dynamic row but I am having problems converting it to sum a column and insert the formula so that the total changes if the numbers change. Is there an example of this anywhere? Thanks

    • Hello, EG Barber thank you for your question. You can use the following code and paste inside your sheet (right click on the sheet, select View Code and paste the code there) to get an automatic VBA sum feature.

      Private Sub Worksheet_Change(ByVal Target As Range)
      Dim our_cell_range As Range
      Set our_cell_range = Range("C5:C10")
      If Not Application.Intersect(our_cell_range, Range(Target.Address)) Is Nothing Then
      Range("C11") = Application.WorksheetFunction.Sum(our_cell_range)
      End If
      End Sub

      The following animated image shows the solution in action.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo