Sum a Dynamic range of cells in the same row with VBA

nmounir

Board Regular
Joined
Oct 16, 2020
Messages
107
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,

I new to VBA and was looking online on how to sum the values in the same row using VBA, i only found on the same column but nothing for the same row.
Cell C2 = 20, Cell D2 = 30, Cell E2 = 40 etc. More values can be added to to cell F2 and G2 etc.

I used the following code but is not working:

Sub Test()

Dim LastCell As Integer
Dim I As Long
LastCell = Range("C2").End(xlToRight).Column
For I = 2 To LastCell
Range("B13").Value = Excel.WorksheetFunction.Sum(Range("C2 & I"))

Next I

End Sub

Any help?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
How about
VBA Code:
Sub nmounir()
   Range("B13").Value = Application.Sum(Range(Range("C2"), Cells(2, Columns.Count).End(xlToLeft)))
End Sub
 
Upvote 0
How about
VBA Code:
Sub nmounir()
   Range("B13").Value = Application.Sum(Range(Range("C2"), Cells(2, Columns.Count).End(xlToLeft)))
End Sub
Fluff,

you are genius. Thanks a million

I tried this code earlier on but also didn't work. any idea what could be wrong with it?
the current region in the immediate window is giving me the correct address which is $C$2:$I$2 then i just put Sum(rng)


Sub Sum()
Dim Rng As String
Rng = Range("C2").CurrentRegion.Address
Range("B13").Value = Excel.WorksheetFunction.Sum(Rng)

End Sub
 
Upvote 0
You need to pass the range object, not the address like
VBA Code:
Sub Sum()
Dim Rng As Range
Set Rng = Range("C2").CurrentRegion
Range("B13").Value = WorksheetFunction.Sum(Rng)

End Sub
 
Upvote 0
Solution
You need to pass the range object, not the address like
VBA Code:
Sub Sum()
Dim Rng As Range
Set Rng = Range("C2").CurrentRegion
Range("B13").Value = WorksheetFunction.Sum(Rng)

End Sub
Thank you so much Fluff. Much appreciated
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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