PDA

View Full Version : Solved: summing range



satyen
03-23-2010, 05:23 AM
I am trying to sum data within two ranges. It keeps coming up with Object Defined error

ActiveCell.Resize(, vCol).Formula = "=SUM(" & vRow & t.Row - 2 & ")"

Please can someone help

Many Thanks!

mdmackillop
03-23-2010, 05:33 AM
Try
ActiveCell.Resize(, vCol).Formula = "SUM(" & vRow & t.Row - 2 & ")"

You'll see the formula without the = sign, and it should show what is missing. There should be a colon in there somewhere.

Bob Phillips
03-23-2010, 06:09 AM
Maybe



ActiveCell.Resize(, vCol).Formula = "=SUM(" & vRow & ":" & t.Row - 2 & ")"

satyen
03-24-2010, 07:56 AM
you need the = sign mdmackillop (http://www.vbaexpress.com/forum/member.php?u=87).

I think I will post the whole piece of code a bit later as its not working as I would like it to.

mdmackillop
03-24-2010, 09:30 AM
The purpose in omitting = is to show the error in the formula.

SamT
03-24-2010, 12:09 PM
vRow t.Row
What's up with the mismatched dots?

Bob Phillips
03-24-2010, 12:41 PM
The purpose in omitting = is to show the error in the formula.

:)

satyen
03-26-2010, 01:37 AM
ActiveCell.Resize(, vCol - 2).Formula = "=SUM(" & vRow & ":" & t.Row & ")"

This does not come up with an error now but doesn't sum a line of data, instead the range in all 15 columns. I need to add a column

would it be something like "B2:vRow & ":" & t.Row & ")"

satyen
03-26-2010, 01:41 AM
actually something like this as I want it to start from vRow, but this doesn't work
ActiveCell.Resize(, vCol - 2).Formula = "=SUM(B":vRow " & ":" & t.Row & ")"

Bob Phillips
03-26-2010, 03:17 AM
Maybe



ActiveCell.Resize(, vCol - 2).Formula = "=SUM(B":vRow " & ":T" & vRow & ")"

satyen
03-26-2010, 03:28 AM
expected end of statement error with this one

Bob Phillips
03-26-2010, 03:30 AM
Oops I copied your typo



ctiveCell.Resize(, vCol - 2).Formula = "=SUM(B":" & vRow " & ":T" & vRow & ")"

satyen
03-26-2010, 03:33 AM
expected line no. or statement or end of statement error

SamT
03-26-2010, 04:36 AM
ActiveCell.Resize(, vCol - 2).Formula = "=SUM(B" & vRow " & ":T" & vRow & ")"

Bob Phillips
03-26-2010, 04:36 AM
I'm having a bad day



ActiveCell.Resize(, vCol - 2).Formula = "=SUM(B" & vRow & ":T" & vRow & ")"

satyen
03-26-2010, 05:57 AM
its not quite doing what I want it to do..
its summing a whole row than a column. I want to sum from top to bottom vrow down to t.row

SamT
03-26-2010, 06:17 AM
ActiveCell.Resize(, vCol - 2).Formula = "=SUM(B" & vRow & ":B" & tRow & ")"

satyen
03-26-2010, 07:06 AM
Here is the code in it’s entirety, may be it may be easier to understand with the whole piece of code. I’ve commented some lines to make easier to understand the problem.

Sub mTest()

Sheets("All").Activate
'Totaling
Dim vCol As Long
Dim vRow As Long
Dim t As Range

vCol = Cells(3, Columns.Count).End(xlToLeft).Column '3=Start at First column heading row
vRow = Cells(Rows.Count, 1).End(xlUp).Row
Cells.Find(What:="Calls", After:=ActiveCell, LookIn _
:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).End(xlDown).Offset(3, 0).Activate 'this will find where to insert "Sub Totals:"

ActiveCell.Formula = "Sub Totals:"
Selection.Font.Bold = True
ActiveCell.Offset(0, 1).Activate

Set t = Columns(1).Find("Calls:").Offset(1, 1) 'everything from here should be totaled uptil vRow and then copied accross
ActiveCell.Resize(, vCol - 2).Formula = "=SUM(B" & vRow & ":B" & t & ")"
Range("A1").Select
End Sub

Bob Phillips
03-26-2010, 07:54 AM
Still guessing



Sub mTest()

Sheets("All").Activate
'Totaling
Dim vCol As Long
Dim vRow As Long
Dim t As Range

vCol = Cells(3, Columns.Count).End(xlToLeft).Column '3=Start at First column heading row
vRow = Cells(Rows.Count, 1).End(xlUp).Row
Cells.Find(What:="Calls", After:=ActiveCell, LookIn _
:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).End(xlDown).Offset(3, 0).Activate 'this will find where to insert "Sub Totals:"

ActiveCell.Formula = "Sub Totals:"
Selection.Font.Bold = True
ActiveCell.Offset(0, 1).Activate

Set t = Columns(1).Find("Calls:").Offset(1, 1) 'everything from here should be totaled uptil vRow and then copied accross
ActiveCell.Resize(, vCol - 2).Formula = "=SUM(B" & vRow & ":B" & t.Row & ")"
Range("A1").Select

End Sub

satyen
03-26-2010, 08:07 AM
yay! this worked! thank you soo much for your effort!

SamT
03-26-2010, 08:48 AM
.

satyen
03-29-2010, 11:37 AM
thank you all for your effort on this.