worksheetfunction.sum - using defined range

Blunder1

Active Member
Joined
Jun 2, 2010
Messages
250
Hi,

I need to select a range and call it rng, say a1, e1, g1, and i1 then in vba sum this using the worksheetfunction,sum(rng). However it is only pickihn up the first cell. My code and i'm struggling to correct it.

Thanks in advance

Blunder

Code:
Sub sumcells()
Dim r As Integer
Dim rng As Range
r = ActiveCell.Row
rng = Range("a" & r & "," & "e" & r & "," & "g" & r & "," & "i" & r)
ActiveCell.Value = WorksheetFunction.Sum(rng)
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You were so close but forgot to set the range:
Code:
Sub sumcells()
Dim r As Integer
Dim rng As Range
r = ActiveCell.Row
Set rng = Range("a" & r & "," & "e" & r & "," & "g" & r & "," & "i" & r)    'notice the "Set"
ActiveCell.Value = WorksheetFunction.Sum(rng)
End Sub
 
Upvote 0
Try

Rich (BB code):
Sub sumcells()
Dim r As Integer
Dim rng As Range
r = ActiveCell.Row
Set rng = Range("a" & r & "," & "e" & r & "," & "g" & r & "," & "i" & r)
ActiveCell.Value = WorksheetFunction.Sum(rng)
End Sub
 
Upvote 0
an hour spent chopping, changing and all i was missign was 'Set'!!!!!

thanks for you fast responses, much appreciated

Blunder
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,348
Members
452,907
Latest member
Roland Deschain

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