LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sum row using variable

It's difficult to know where this formula goes since you use Activecell.

But maybe this will help:

Option Explicit
Sub testme()

Dim LastRow As Long
Dim FirstRow As Long
Dim ThisCol As Long

With Worksheets("sheet1")
ThisCol = 23 '???
FirstRow = 2
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range(.Cells(FirstRow, ThisCol), .Cells(LastRow, ThisCol)).FormulaR1C1 _
= "=SUM(RC[-4]:RC[-1])"
End With

End Sub


BSII wrote:

I am having an issue finding the right code to sum a row of numbers.
Overall, I have a row which is variable in column quantity (cell1 to cell2)
and I want to have the VBA automatically put the formula "=Sum(cell1:cell2)"
into the first free cell at the end of the row.

I can't have the macro enter in the actual summed value as we will need to
add some of the data after the code is run, so I really need the actual
"=Sum(cell1:cell2)" formula into the cell so that it caluclates on the fly.
I've tried several different combinations using R1C1 with varible offsets and
basic forumla functions using the variables as the cell address, but the
"Sum" function and R1C1 functions seem to be very specific about what it will
take in terms of variables.

I have tried the following:

sub row_totals ()

Dim TotRange as Range
Dim TotStartAdd as String
Dim TotEndAdd as String
Dim TotFirstCol as Integer
Dim TotLastCol as Integer
Dim TotRow as Integer
Dim LC as Integer

LC = 22 'last column of used data calculated elsewhere
TotRow = Activecell.row

TotFirstCol = LC - (LC-4) 'first column to be summed
TotLastCol = LC 'Last Column to be summed

TotStartAdd = Cells(TotRow, TotFirstCol).address 'Cell1 address
TotEndAdd = Cells(TotRow, TotLastCol).address 'Cell2 address

Activecell.formula = "=sum("TotStartAdd" : "TotEndAdd")"

End Sub

This doesn't work and I'm sure I'm making this much more complicated than it
needs to be. Any help would be appreciated.


--

Dave Peterson
 
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
variable height variable width stacked bar charts ambthiru Charts and Charting in Excel 3 January 18th 06 11:41 PM
Run-time Error'91: Object variable or With block variable not set DynamiteSkippy Excel Programming 4 September 26th 05 07:47 AM
why is it saying sheetcnt is "variable not defined" how to do a global variable to share over multiple functions in vba for excel? Daniel Excel Worksheet Functions 1 July 9th 05 03:05 AM
Run-time error '91': "Object variable or With block variable not set Mike[_92_] Excel Programming 2 December 30th 04 10:59 AM
Cells.Find error Object variable or With block variable not set Peter[_21_] Excel Programming 2 May 8th 04 02:15 PM


All times are GMT +1. The time now is 11:56 PM.

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"