PC Review


Reply
Thread Tools Rate Thread

Code to calulate percent of total in column

 
 
Scott
Guest
Posts: n/a
 
      13th Oct 2006
Hello-

A5:A14 contain number values, and A15 is the total of these numbers. In
column B, I need to figure the percent of total for the numbers in
column A. For this example, B5 would = A5/A15, B6 would = A6/A15, etc,
etc.
The amount of rows in column A will change each time I run the report
so I need the code to start in B5 and fill down to the next to last
value in column as (since the last value is the total of column A). Any
help will be appreciated.

Scott

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sk5X?=
Guest
Posts: n/a
 
      13th Oct 2006
The following should do the trick

dim Total as double
dim cell as range

total = range("A15").value

For each cell in range("B5:B14")
cell.value = (cell.offset(0,1).value / Total)
next cell
--
JNW


"Scott" wrote:

> Hello-
>
> A5:A14 contain number values, and A15 is the total of these numbers. In
> column B, I need to figure the percent of total for the numbers in
> column A. For this example, B5 would = A5/A15, B6 would = A6/A15, etc,
> etc.
> The amount of rows in column A will change each time I run the report
> so I need the code to start in B5 and fill down to the next to last
> value in column as (since the last value is the total of column A). Any
> help will be appreciated.
>
> Scott
>
>

 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      13th Oct 2006
This does not look like it will handle the changes in the amount of
rows in column A though. It was A5:A15 for the example, but it might be
A5:A22 one day and A5:A37 the next....

JNW wrote:
> The following should do the trick
>
> dim Total as double
> dim cell as range
>
> total = range("A15").value
>
> For each cell in range("B5:B14")
> cell.value = (cell.offset(0,1).value / Total)
> next cell
> --
> JNW
>
>
> "Scott" wrote:
>
> > Hello-
> >
> > A5:A14 contain number values, and A15 is the total of these numbers. In
> > column B, I need to figure the percent of total for the numbers in
> > column A. For this example, B5 would = A5/A15, B6 would = A6/A15, etc,
> > etc.
> > The amount of rows in column A will change each time I run the report
> > so I need the code to start in B5 and fill down to the next to last
> > value in column as (since the last value is the total of column A). Any
> > help will be appreciated.
> >
> > Scott
> >
> >


 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      13th Oct 2006
This code assumes that the last populated cell in column A is the Total (A15
in your example)...

sub Whatever()
dim rngAllValues as Range
dim rngCurrent as Range
dim wks as worksheet
dim dblTotal as Double

set wks = sheets("Sheet1")
with wks
set rngallvalue = .range(.range("A5"), _
.cells(rows.count, "A").end(xlup).offset(-1, 0))
end with
dblTotal = application.sum(rngallvalues)
for each rngcurrent in rngallvalues
rngcurrent.offset(0,1).value = rngcurrent.value / dbltotal
next rngcurrent
end sub

--
HTH...

Jim Thomlinson


"Scott" wrote:

> This does not look like it will handle the changes in the amount of
> rows in column A though. It was A5:A15 for the example, but it might be
> A5:A22 one day and A5:A37 the next....
>
> JNW wrote:
> > The following should do the trick
> >
> > dim Total as double
> > dim cell as range
> >
> > total = range("A15").value
> >
> > For each cell in range("B5:B14")
> > cell.value = (cell.offset(0,1).value / Total)
> > next cell
> > --
> > JNW
> >
> >
> > "Scott" wrote:
> >
> > > Hello-
> > >
> > > A5:A14 contain number values, and A15 is the total of these numbers. In
> > > column B, I need to figure the percent of total for the numbers in
> > > column A. For this example, B5 would = A5/A15, B6 would = A6/A15, etc,
> > > etc.
> > > The amount of rows in column A will change each time I run the report
> > > so I need the code to start in B5 and fill down to the next to last
> > > value in column as (since the last value is the total of column A). Any
> > > help will be appreciated.
> > >
> > > Scott
> > >
> > >

>
>

 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      14th Oct 2006
Jim-

I am getting an error in the following line:

dblTotal = Application.Sum(rngAllValues)

Any ideas?

Jim Thomlinson wrote:
> This code assumes that the last populated cell in column A is the Total (A15
> in your example)...
>
> sub Whatever()
> dim rngAllValues as Range
> dim rngCurrent as Range
> dim wks as worksheet
> dim dblTotal as Double
>
> set wks = sheets("Sheet1")
> with wks
> set rngallvalue = .range(.range("A5"), _
> .cells(rows.count, "A").end(xlup).offset(-1, 0))
> end with
> dblTotal = application.sum(rngallvalues)
> for each rngcurrent in rngallvalues
> rngcurrent.offset(0,1).value = rngcurrent.value / dbltotal
> next rngcurrent
> end sub
>
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Scott" wrote:
>
> > This does not look like it will handle the changes in the amount of
> > rows in column A though. It was A5:A15 for the example, but it might be
> > A5:A22 one day and A5:A37 the next....
> >
> > JNW wrote:
> > > The following should do the trick
> > >
> > > dim Total as double
> > > dim cell as range
> > >
> > > total = range("A15").value
> > >
> > > For each cell in range("B5:B14")
> > > cell.value = (cell.offset(0,1).value / Total)
> > > next cell
> > > --
> > > JNW
> > >
> > >
> > > "Scott" wrote:
> > >
> > > > Hello-
> > > >
> > > > A5:A14 contain number values, and A15 is the total of these numbers. In
> > > > column B, I need to figure the percent of total for the numbers in
> > > > column A. For this example, B5 would = A5/A15, B6 would = A6/A15, etc,
> > > > etc.
> > > > The amount of rows in column A will change each time I run the report
> > > > so I need the code to start in B5 and fill down to the next to last
> > > > value in column as (since the last value is the total of column A). Any
> > > > help will be appreciated.
> > > >
> > > > Scott
> > > >
> > > >

> >
> >


 
Reply With Quote
 
Gary Keramidas
Guest
Posts: n/a
 
      14th Oct 2006
add an s to this variable

set rngallvalue =
so it look like this
set rngallvalues =

--


Gary


"Scott" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Jim-
>
> I am getting an error in the following line:
>
> dblTotal = Application.Sum(rngAllValues)
>
> Any ideas?
>
> Jim Thomlinson wrote:
>> This code assumes that the last populated cell in column A is the Total (A15
>> in your example)...
>>
>> sub Whatever()
>> dim rngAllValues as Range
>> dim rngCurrent as Range
>> dim wks as worksheet
>> dim dblTotal as Double
>>
>> set wks = sheets("Sheet1")
>> with wks
>> set rngallvalue = .range(.range("A5"), _
>> .cells(rows.count, "A").end(xlup).offset(-1, 0))
>> end with
>> dblTotal = application.sum(rngallvalues)
>> for each rngcurrent in rngallvalues
>> rngcurrent.offset(0,1).value = rngcurrent.value / dbltotal
>> next rngcurrent
>> end sub
>>
>> --
>> HTH...
>>
>> Jim Thomlinson
>>
>>
>> "Scott" wrote:
>>
>> > This does not look like it will handle the changes in the amount of
>> > rows in column A though. It was A5:A15 for the example, but it might be
>> > A5:A22 one day and A5:A37 the next....
>> >
>> > JNW wrote:
>> > > The following should do the trick
>> > >
>> > > dim Total as double
>> > > dim cell as range
>> > >
>> > > total = range("A15").value
>> > >
>> > > For each cell in range("B5:B14")
>> > > cell.value = (cell.offset(0,1).value / Total)
>> > > next cell
>> > > --
>> > > JNW
>> > >
>> > >
>> > > "Scott" wrote:
>> > >
>> > > > Hello-
>> > > >
>> > > > A5:A14 contain number values, and A15 is the total of these numbers. In
>> > > > column B, I need to figure the percent of total for the numbers in
>> > > > column A. For this example, B5 would = A5/A15, B6 would = A6/A15, etc,
>> > > > etc.
>> > > > The amount of rows in column A will change each time I run the report
>> > > > so I need the code to start in B5 and fill down to the next to last
>> > > > value in column as (since the last value is the total of column A). Any
>> > > > help will be appreciated.
>> > > >
>> > > > Scott
>> > > >
>> > > >
>> >
>> >

>



 
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
Calculating percent of times a value is in a column and total # clbritt76 Microsoft Excel Worksheet Functions 2 26th Mar 2010 01:23 PM
Access 2007 Visual Basic Running Total and Percent of Total.... Nikolai Microsoft Access VBA Modules 0 13th Nov 2008 03:10 PM
Calulate total prior to a given date =?Utf-8?B?Tmljaw==?= Microsoft Access Queries 3 7th Nov 2007 06:56 AM
I am trying to calulate total time =?Utf-8?B?S2FybA==?= Microsoft Excel Worksheet Functions 3 13th Jul 2006 09:41 PM
Add a "Percent of Total of an answer" column in a pivot table =?Utf-8?B?V2lsbFJu?= Microsoft Excel Misc 2 1st Sep 2004 02:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:15 PM.