PC Review


Reply
Thread Tools Rate Thread

adding all the cells in a group of cells

 
 
MarkS
Guest
Posts: n/a
 
      18th Aug 2008
Hi,
I need to know the sum of a group of cells I us this piece of code to do
this job

dTotal = 0
iRowCounter1 = 2
iColumnCounter1 = 5
Do Until Sheets("Futures").Cells(iRowCounter1, iColumnCounter1).Value = ""
Do Until Sheets("Futures").Cells(iRowCounter1, iColumnCounter1).Value = ""
dTotal = dTotal + Sheets("Futures").Cells(iRowCounter1,
iColumnCounter1).Value
iRowCounter1 = iRowCounter1 + 1
Loop
iRowCounter1 = 2
iColumnCounter1 = iColumnCounter1 + 1
Loop

Does any one know how to do this, without looping through all the cells, as
it is always the same size

Thanks MarkS

 
Reply With Quote
 
 
 
 
OssieMac
Guest
Posts: n/a
 
      18th Aug 2008
Hi Mark,

Three options. first two basically the same but the first one finds the last
cell of the range first before assigning the entire range to a variable.

The second option does the above in one line of code.

Third option is just simple code if you actually know the range.

The space and underscore at the end of lines are line breaks in otherwise
single lines of code. (Just in case you are not aware of this)

Sub Sum_a_Range_Option1()

Dim dTotal As Long
Dim iRowCounter1 As Long
Dim iColumnCounter1 As Long
Dim rngToSum As Range
Dim rngLastCell As Range

dTotal = 0
iRowCounter1 = 2
iColumnCounter1 = 5

With Sheets("Futures")

'Assign last cell to a range variable
Set rngLastCell = .Cells(iRowCounter1, _
iColumnCounter1).End(xlDown).End(xlToRight)

'Assign the entire range to a range variable
Set rngToSum = Range(.Cells(iRowCounter1, _
iColumnCounter1), rngLastCell)

End With

dTotal = WorksheetFunction.Sum(rngToSum)

End Sub

Sub Sum_a_Range_Option2()

Dim dTotal As Long
Dim iRowCounter1 As Long
Dim iColumnCounter1 As Long
Dim rngToSum As Range

dTotal = 0
iRowCounter1 = 2
iColumnCounter1 = 5

With Sheets("Futures")

'Assign the entire range to a range variable
'using only one line of code
Set rngToSum = Range(.Cells(iRowCounter1, _
iColumnCounter1), .Cells(iRowCounter1, _
iColumnCounter1).End(xlDown).End(xlToRight))

End With

dTotal = WorksheetFunction.Sum(rngToSum)

End Sub

Sub Sum_a_Range_Option3()

Dim dTotal As Long

dTotal = WorksheetFunction.Sum(Sheets("Futures") _
.Range("E2:I15"))


End Sub


--
Regards,

OssieMac


"MarkS" wrote:

> Hi,
> I need to know the sum of a group of cells I us this piece of code to do
> this job
>
> dTotal = 0
> iRowCounter1 = 2
> iColumnCounter1 = 5
> Do Until Sheets("Futures").Cells(iRowCounter1, iColumnCounter1).Value = ""
> Do Until Sheets("Futures").Cells(iRowCounter1, iColumnCounter1).Value = ""
> dTotal = dTotal + Sheets("Futures").Cells(iRowCounter1,
> iColumnCounter1).Value
> iRowCounter1 = iRowCounter1 + 1
> Loop
> iRowCounter1 = 2
> iColumnCounter1 = iColumnCounter1 + 1
> Loop
>
> Does any one know how to do this, without looping through all the cells, as
> it is always the same size
>
> Thanks MarkS
>

 
Reply With Quote
 
Nayab
Guest
Posts: n/a
 
      18th Aug 2008
On Aug 18, 10:21*am, MarkS <Ma...@discussions.microsoft.com> wrote:
> Hi,
> I need to know the sum of a group of cells I us this piece of code to do
> this job
>
> dTotal = 0
> iRowCounter1 = 2
> iColumnCounter1 = 5
> Do Until Sheets("Futures").Cells(iRowCounter1, iColumnCounter1).Value =""
> * Do Until Sheets("Futures").Cells(iRowCounter1, iColumnCounter1).Value= ""
> * *dTotal = dTotal + Sheets("Futures").Cells(iRowCounter1,
> iColumnCounter1).Value
> * *iRowCounter1 = iRowCounter1 + 1
> * Loop
> iRowCounter1 = 2
> iColumnCounter1 = iColumnCounter1 + 1
> Loop
>
> Does any one know how to do this, without looping through all the cells, as
> it is always the same size
>
> Thanks MarkS


why not use the the sum method of excel:

u can calculate the sum in some remote cell may be
Range("IV65536").value and then make dTotal equal to this value and
then clear contents from IV65536.
 
Reply With Quote
 
kounoike
Guest
Posts: n/a
 
      18th Aug 2008
Hi
If your data start at Cells(2,5) and consecutive(no empty cells between
data) in both column's and row's direction , Macro as below would work.

Sub sumtest0()
Dim rng As Range

On Error Resume Next
Set rng = Range(Cells(2, 5), Cells(Cells.Rows.Count, _
Cells.Columns.Count)).SpecialCells(xlCellTypeConstants, 1)
rng.Select '<--- just for checking the range of sum
If Not rng Is Nothing Then
MsgBox Application.Sum(rng)
Else
MsgBox "Nothing to sum"
End If
End Sub

If your data has empty cells between data, the Macro above would not work,
instead, try this one.
But i don't know this one is as efficient as your code.
Be sure that Sheets("Futures") is selected before running both macros

Sub sumtest1()
Dim clast As Range, rlast As Range, clrng As Range
Dim cell As Range, sumrng As Range

If Cells(2, 5) = "" Then
MsgBox "Nothing to sum"
Exit Sub
Else
If Cells(2, 5).Offset(0, 1) = "" Then
Set clast = Cells(2, 5)
Set clrng = clast
Else
Set clast = Cells(2, 5).End(xlToRight)
Set clrng = Range(Cells(2, 5), clast)
End If
End If

Set sumrng = Nothing

For Each cell In clrng
If cell.Offset(1, 0) = "" Then
If sumrng Is Nothing Then
Set sumrng = cell
Else
Set sumrng = Union(sumrng, cell)
End If
Else
Set rlast = cell.End(xlDown)
If sumrng Is Nothing Then
Set sumrng = Range(cell, rlast)
Else
Set sumrng = Union(sumrng, Range(cell, rlast))
End If
End If
Next
sumrng.Select '<--- just for checking the range of sum
MsgBox Application.Sum(sumrng)
End Sub

keiji

"MarkS" <(E-Mail Removed)> wrote in message
newsA85A7C2-A95E-4015-B9BE-(E-Mail Removed)...
> Hi,
> I need to know the sum of a group of cells I us this piece of code to do
> this job
>
> dTotal = 0
> iRowCounter1 = 2
> iColumnCounter1 = 5
> Do Until Sheets("Futures").Cells(iRowCounter1, iColumnCounter1).Value = ""
> Do Until Sheets("Futures").Cells(iRowCounter1, iColumnCounter1).Value =
> ""
> dTotal = dTotal + Sheets("Futures").Cells(iRowCounter1,
> iColumnCounter1).Value
> iRowCounter1 = iRowCounter1 + 1
> Loop
> iRowCounter1 = 2
> iColumnCounter1 = iColumnCounter1 + 1
> Loop
>
> Does any one know how to do this, without looping through all the cells,
> as
> it is always the same size
>
> Thanks MarkS
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding comment to cells not working with merged cells =?Utf-8?B?bWNwaGM=?= Microsoft Excel Programming 2 29th Aug 2007 07:09 PM
Adding zero's to a group of cells =?Utf-8?B?RGVzaXJlZQ==?= Microsoft Excel Misc 5 29th Jul 2005 07:07 PM
Adding colour to a range of cells based on one of the cells v... =?Utf-8?B?TWNLZW5uYQ==?= Microsoft Excel Misc 4 11th Mar 2005 02:25 PM
Adding a certain number from a group of cells eric7777 Microsoft Excel Worksheet Functions 1 12th Feb 2004 07:59 AM
How to find values for a group of cells based on another group of cells Mike Microsoft Excel Worksheet Functions 0 9th Sep 2003 08:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:14 AM.