Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Newbie : How to sum cells via VBA code.

I am using Excel 97.

I want to sum the values of several cells. I tried to follow the
examples that I see in this forum and they don't work. My code looks
like :

lcRange is equal to B3:B35

ActiveCell.Value = "=sum(" & lcRange & ")"

I thought that the above command would sum up the values in a column
like an auto sum. But it does not work. Can anyone help me ? How can I
sum ???

Rich
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Newbie : How to sum cells via VBA code.

Hey Rich

Tr

ActiveCell.Formula = "=sum(lcRange)

You have to tell Excel it's a formula

Good luck
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default Newbie : How to sum cells via VBA code.

Hi Rich

Is there a special reason of not doing this the 'normal' way

A more quicker method if you only want to store a hard-coded value is to do the sum in VBA and then output it to your "activecell" like this

-------------------------------------------
Sub Sum(

Dim Result As Intege
Dim lcRange As Rang

Set lcRange = Range("B3:B35"

Result = Application.WorksheetFunction.Sum(lcRange
ActiveCell.Value = Resul

End Su
-------------------------------------------

SuperJas.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default Newbie : How to sum cells via VBA code.

Rich

works for me:

Sub test2()
lcRange = "B3:B35"
ActiveCell.Value = "=sum(" & lcRange & ")"
End Sub

Regards

Trevor


"Rich" wrote in message
om...
I am using Excel 97.

I want to sum the values of several cells. I tried to follow the
examples that I see in this forum and they don't work. My code looks
like :

lcRange is equal to B3:B35

ActiveCell.Value = "=sum(" & lcRange & ")"

I thought that the above command would sum up the values in a column
like an auto sum. But it does not work. Can anyone help me ? How can I
sum ???

Rich



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Newbie : How to sum cells via VBA code.

Rich,

More flexible

lcRange = "B3:B" & Cells(Rows.Count,"B").End(xlUp).Row
ActiveCell.Formula= "=sum(" & lcRange & ")"

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Rich" wrote in message
om...
I am using Excel 97.

I want to sum the values of several cells. I tried to follow the
examples that I see in this forum and they don't work. My code looks
like :

lcRange is equal to B3:B35

ActiveCell.Value = "=sum(" & lcRange & ")"

I thought that the above command would sum up the values in a column
like an auto sum. But it does not work. Can anyone help me ? How can I
sum ???

Rich





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Newbie : How to sum cells via VBA code.

There are a lot of ways to do this but here are two
examples:

Sub SumRoutine01()

' dimension range of values to sum

Dim SumRange As Range

' give the active sheet a name to make this
' more useful in the future
Set currentBook = Application.ActiveWorkbook
thissheet = ActiveSheet.Name
Worksheets(thissheet).Activate

Set SumRange = Range(Worksheets(thissheet).Cells(3, 2),
Worksheets(thissheet).Cells(35, 2))

Worksheets(thissheet).Cells(1, 1).Value =
Application.WorksheetFunction.Sum(SumRange)


End Sub

Sub SumRoutine02()

' dimension the variables that can be used
' to sum the values.

Dim summer, cellvalue As Single

' give the active sheet a name to make this
' more useful in the future
Set currentBook = Application.ActiveWorkbook
thisSheet = ActiveSheet.Name
Worksheets(thisSheet).Activate

' zero the sum variable
summer = 0

' loop to sum the raw values
' I used 3 to 35 to match the row indexes you
' specified. Cells(row,column)

For i = 3 To 35
cellvalue = Worksheets(thisSheet).Cells(i, 2).Value
summer = summer + cellvalue
Next i

Worksheets(thisSheet).Cells(1, 1).Value = summer

End Sub


Hope these help.
Raul

-----Original Message-----
I am using Excel 97.

I want to sum the values of several cells. I tried to

follow the
examples that I see in this forum and they don't work.

My code looks
like :

lcRange is equal to B3:B35

ActiveCell.Value = "=sum(" & lcRange & ")"

I thought that the above command would sum up the values

in a column
like an auto sum. But it does not work. Can anyone help

me ? How can I
sum ???

Rich
.

Reply
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
[newbie] copy unhidden cells Jon Excel Discussion (Misc queries) 1 April 2nd 09 05:09 PM
Newbie Date cells so most recent is on top [email protected] Excel Worksheet Functions 1 May 14th 06 02:48 AM
VBA Newbie: Help with Do Loop code Carl Excel Discussion (Misc queries) 3 December 2nd 04 07:04 PM
Newbie : Help with sort via Excel code Rich[_16_] Excel Programming 1 January 10th 04 12:20 AM
Newbie : Autofilter thru code ? Rich[_16_] Excel Programming 2 October 5th 03 07:27 PM


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

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

About Us

"It's about Microsoft Excel"