PC Review


Reply
Thread Tools Rate Thread

Count Num and Sum functions as a variable

 
 
Chris Freeman
Guest
Posts: n/a
 
      23rd Jun 2009

I have a sheet that I need to count the number of items in a column and also
sum them. I can add a formula to cell, collect the values to a variable, then
clear the cell, but I was trying to figure out a more elegant internal code.
I wanted to caputure the number of items in the column without looping.
problem is that some rows are blank, and Selection.Rows.Count brings back all
rows.

Thanks
---------------------------------
Chris Freeman
IT Project Coordinator
 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      23rd Jun 2009

Look at the WorksheetFunction Property (of the Application object)... the
count and sum functions are all available through it.

--
Rick (MVP - Excel)


"Chris Freeman" <(E-Mail Removed)> wrote in message
news:1776B21A-0E5E-4EC8-BD99-(E-Mail Removed)...
>I have a sheet that I need to count the number of items in a column and
>also
> sum them. I can add a formula to cell, collect the values to a variable,
> then
> clear the cell, but I was trying to figure out a more elegant internal
> code.
> I wanted to caputure the number of items in the column without looping.
> problem is that some rows are blank, and Selection.Rows.Count brings back
> all
> rows.
>
> Thanks
> ---------------------------------
> Chris Freeman
> IT Project Coordinator


 
Reply With Quote
 
marcus
Guest
Posts: n/a
 
      23rd Jun 2009
Hi Chris

This should place the number of used cells after the last used cell in
Column A. It looks at the rows in Column A2 to the last used cell and
counts only those rows which are not empty. You can adapt to suit.

Take care

Marcus

Sub CountUsed()
Dim Test As Integer
Dim lw As Integer
lw = Range("A" & Rows.Count).End(xlUp).Row
Test = Application.WorksheetFunction.CountA(Range("A2:A" & lw))
Range("A" & lw + 1).Value = Test
End Sub
 
Reply With Quote
 
Chris Freeman
Guest
Posts: n/a
 
      23rd Jun 2009

Rick,
I have no idea what you just said. I know a little programming but, what is
WorksheetFunction Property?

I items listed when I press F2 in the code window, but how do I add this to
my code?
--
Chris Freeman
IT Project Coordinator


"Rick Rothstein" wrote:

> Look at the WorksheetFunction Property (of the Application object)... the
> count and sum functions are all available through it.
>
> --
> Rick (MVP - Excel)
>
>
> "Chris Freeman" <(E-Mail Removed)> wrote in message
> news:1776B21A-0E5E-4EC8-BD99-(E-Mail Removed)...
> >I have a sheet that I need to count the number of items in a column and
> >also
> > sum them. I can add a formula to cell, collect the values to a variable,
> > then
> > clear the cell, but I was trying to figure out a more elegant internal
> > code.
> > I wanted to caputure the number of items in the column without looping.
> > problem is that some rows are blank, and Selection.Rows.Count brings back
> > all
> > rows.
> >
> > Thanks
> > ---------------------------------
> > Chris Freeman
> > IT Project Coordinator

>
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      23rd Jun 2009

It's hard to say how you should add this to your code as you didn't post any
code (which is why I gave you the general-type answer I did. Here is a
made-up example that may help you see how to integrate it into your code.

RangeTotalVariable = WorksheetFunction.Sum(Range("A1:A100"))

CountNumbersVariable = WorksheetFunction.Count(Range("A1:A100"))

CountTextCellsVariable = WorksheetFunction.CountA(Range("A1:A100"))

Of course, these constructions can be more complicated than I have shown
depending on what you are trying to do.

--
Rick (MVP - Excel)


"Chris Freeman" <(E-Mail Removed)> wrote in message
news:BDD55956-4E54-4BBF-BEDA-(E-Mail Removed)...
> Rick,
> I have no idea what you just said. I know a little programming but, what
> is
> WorksheetFunction Property?
>
> I items listed when I press F2 in the code window, but how do I add this
> to
> my code?
> --
> Chris Freeman
> IT Project Coordinator
>
>
> "Rick Rothstein" wrote:
>
>> Look at the WorksheetFunction Property (of the Application object)... the
>> count and sum functions are all available through it.
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>> "Chris Freeman" <(E-Mail Removed)> wrote in message
>> news:1776B21A-0E5E-4EC8-BD99-(E-Mail Removed)...
>> >I have a sheet that I need to count the number of items in a column and
>> >also
>> > sum them. I can add a formula to cell, collect the values to a
>> > variable,
>> > then
>> > clear the cell, but I was trying to figure out a more elegant internal
>> > code.
>> > I wanted to caputure the number of items in the column without
>> > looping.
>> > problem is that some rows are blank, and Selection.Rows.Count brings
>> > back
>> > all
>> > rows.
>> >
>> > Thanks
>> > ---------------------------------
>> > Chris Freeman
>> > IT Project Coordinator

>>
>>


 
Reply With Quote
 
Chris Freeman
Guest
Posts: n/a
 
      23rd Jun 2009

Rick,
Perfect, I just couldn't get the syntax right. I kept trying to use
Selection.rows.count and that just brought back all rows in the selected
area. Thanks, and I'll try to be more precise and include code next time.

Thanks
--
Chris Freeman
IT Project Coordinator


"Rick Rothstein" wrote:

> It's hard to say how you should add this to your code as you didn't post any
> code (which is why I gave you the general-type answer I did. Here is a
> made-up example that may help you see how to integrate it into your code.
>
> RangeTotalVariable = WorksheetFunction.Sum(Range("A1:A100"))
>
> CountNumbersVariable = WorksheetFunction.Count(Range("A1:A100"))
>
> CountTextCellsVariable = WorksheetFunction.CountA(Range("A1:A100"))
>
> Of course, these constructions can be more complicated than I have shown
> depending on what you are trying to do.
>
> --
> Rick (MVP - Excel)
>
>
> "Chris Freeman" <(E-Mail Removed)> wrote in message
> news:BDD55956-4E54-4BBF-BEDA-(E-Mail Removed)...
> > Rick,
> > I have no idea what you just said. I know a little programming but, what
> > is
> > WorksheetFunction Property?
> >
> > I items listed when I press F2 in the code window, but how do I add this
> > to
> > my code?
> > --
> > Chris Freeman
> > IT Project Coordinator
> >
> >
> > "Rick Rothstein" wrote:
> >
> >> Look at the WorksheetFunction Property (of the Application object)... the
> >> count and sum functions are all available through it.
> >>
> >> --
> >> Rick (MVP - Excel)
> >>
> >>
> >> "Chris Freeman" <(E-Mail Removed)> wrote in message
> >> news:1776B21A-0E5E-4EC8-BD99-(E-Mail Removed)...
> >> >I have a sheet that I need to count the number of items in a column and
> >> >also
> >> > sum them. I can add a formula to cell, collect the values to a
> >> > variable,
> >> > then
> >> > clear the cell, but I was trying to figure out a more elegant internal
> >> > code.
> >> > I wanted to caputure the number of items in the column without
> >> > looping.
> >> > problem is that some rows are blank, and Selection.Rows.Count brings
> >> > back
> >> > all
> >> > rows.
> >> >
> >> > Thanks
> >> > ---------------------------------
> >> > Chris Freeman
> >> > IT Project Coordinator
> >>
> >>

>
>

 
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
Help With A Variable And Functions Andy Microsoft Excel Programming 3 17th May 2010 10:01 PM
Multiple-variable Functions jtertin Microsoft Access Forms 2 20th Jun 2007 06:55 PM
Variable Functions Sam Bench Microsoft Excel Discussion 2 12th Feb 2007 01:11 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 Microsoft Excel Worksheet Functions 1 9th Jul 2005 03:05 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 Microsoft Excel Discussion 1 9th Jul 2005 01:04 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:19 AM.