PC Review


Reply
Thread Tools Rate Thread

Add formula to cell to sum range of cells

 
 
kevlarmcc
Guest
Posts: n/a
 
      24th Mar 2010
I am trying to write a macro that selects a range of cells and then format a
nearby cell to show the sum of that range. The code i have does so by
selecting the range, naming it, and using the name in the formula. The
problem is that I want to run this for multiple sheets in the workbook. I
can't figure out how to use a variable to name the range and use it in the
sum formula. Using ActiveSheet.name + "Days" in the sum formula creates an
error.

Range("B11").Select
Range(Selection, Selection.End(xlDown).Offset(-1#)).Select

ActiveWorkbook.Names.Add name:=ActiveSheet.name + "Days", _
RefersToR1C1:=Selection

Range("F10").NumberFormat = "0.00%"
Range("F10").FormulaR1C1 = "=Sum(ActiveSheet.name + "Days")"


 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      24th Mar 2010
Not sure what you are looking for, but this will assign a variable to the
range and then name it.

Set sh = Sheets(ActiveSheet.Name)
With sh
Set myVar = Range("B11", Range("B11").End(xlDown).Offset(-1, 0))
ActiveWorkbook.Names.Add Name:=ActiveSheet.Name + "Days", _
RefersToR1C1:=myVar.Address
Range("F10").NumberFormat = "0.00%"
Range("F10") = WorksheetFunction.Sum(myVar)
End With



"kevlarmcc" <(E-Mail Removed)> wrote in message
news:60FA0473-7ABC-4663-857E-(E-Mail Removed)...
>I am trying to write a macro that selects a range of cells and then format
>a
> nearby cell to show the sum of that range. The code i have does so by
> selecting the range, naming it, and using the name in the formula. The
> problem is that I want to run this for multiple sheets in the workbook. I
> can't figure out how to use a variable to name the range and use it in the
> sum formula. Using ActiveSheet.name + "Days" in the sum formula creates an
> error.
>
> Range("B11").Select
> Range(Selection, Selection.End(xlDown).Offset(-1#)).Select
>
> ActiveWorkbook.Names.Add name:=ActiveSheet.name + "Days", _
> RefersToR1C1:=Selection
>
> Range("F10").NumberFormat = "0.00%"
> Range("F10").FormulaR1C1 = "=Sum(ActiveSheet.name + "Days")"
>
>



 
Reply With Quote
 
FSt1
Guest
Posts: n/a
 
      24th Mar 2010
hi
use a variable.
Range("B11").Select
Range(Selection, Selection.End(xlDown).Offset(-1#)).Select
Dim strng As String '**********
strng = ActiveSheet.Name & "Days"

ActiveWorkbook.Names.Add Name:=strng, _
RefersToR1C1:=Selection

Range("F10").NumberFormat = "0.00%"
Range("F10").FormulaR1C1 = "=Sum(" & strng & ")"

regards
FSt1


"kevlarmcc" wrote:

> I am trying to write a macro that selects a range of cells and then format a
> nearby cell to show the sum of that range. The code i have does so by
> selecting the range, naming it, and using the name in the formula. The
> problem is that I want to run this for multiple sheets in the workbook. I
> can't figure out how to use a variable to name the range and use it in the
> sum formula. Using ActiveSheet.name + "Days" in the sum formula creates an
> error.
>
> Range("B11").Select
> Range(Selection, Selection.End(xlDown).Offset(-1#)).Select
>
> ActiveWorkbook.Names.Add name:=ActiveSheet.name + "Days", _
> RefersToR1C1:=Selection
>
> Range("F10").NumberFormat = "0.00%"
> Range("F10").FormulaR1C1 = "=Sum(ActiveSheet.name + "Days")"
>
>

 
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
How can I make a blank cell in a formula cell with a range of cell =?Utf-8?B?Vmk=?= Microsoft Excel Misc 5 21st Jun 2007 02:46 PM
loop through cells in a range and pick up corresponding cell values in another range patrice.cezzar@gmail.com Microsoft Excel Programming 9 19th Oct 2006 05:11 AM
Determine if range has NO Blank Cells without looping through each cell in range Excelenator Microsoft Excel Programming 4 4th Aug 2006 06:30 AM
Copy cells into range of cells until cell change =?Utf-8?B?bWRlYW5kYQ==?= Microsoft Excel Worksheet Functions 1 22nd Apr 2005 08:41 PM
Copy a formula to a range of cells via VB6 using .Range(Cells(row,col), Cells(row,col)).Formula= statement Kevin Microsoft Excel Programming 7 5th Oct 2004 08:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:46 PM.