LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 126
Default adding all the cells in a group of cells

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(xlCellTypeConst ants, 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" wrote in message
...
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


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Match a group of cells with another group of cells Rod in Oz Excel Worksheet Functions 1 October 9th 08 01:45 AM
Allow macro to work for a group of cells but not the other cells [email protected] Excel Programming 7 May 4th 08 12:03 AM
from a group of cells.find average of cells containing values farm Excel Discussion (Misc queries) 1 December 21st 06 08:50 PM
Adding zero's to a group of cells Desiree Excel Discussion (Misc queries) 5 July 29th 05 07:07 PM
copy group of cells to another group of cells using "IF" in third Chuckak Excel Worksheet Functions 2 November 10th 04 06:04 PM


All times are GMT +1. The time now is 12:11 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"