PC Review


Reply
Thread Tools Rate Thread

Counting rows in a column

 
 
Sören_Marodören
Guest
Posts: n/a
 
      27th Mar 2009
Hi,

I use the following line to count rows in a column A
Rows = Range("A" & Rows.Count).End(xlUp).Row
(I dont't know if this is the best way, but it works.)

Now I want to have a for loop that goes from column C to the last column
and count the number of rows in every column.

For Col = 3 to Last_Col
Rows = ....
Cells (1, Col) = Rows
Next

How shall the Row = ... be written to count the number of rows in column Col?
How can it be written to count columns in another worksheet?


Best regards,
/Sören Nilsson
Sweden
 
Reply With Quote
 
 
 
 
Stefi
Guest
Posts: n/a
 
      27th Mar 2009
One way:
RowsNo = Range(Left(Cells(1, Col).Address(False, False), 1 - (Col > 26)) &
Rows.Count).End(xlUp).Row

On Sheet2:
vRows = Worksheets("Sheet2").Range(Left(Cells(1, Col).Address(False, False),
1 - (Col > 26)) & Rows.Count).End(xlUp).Row

Don't use Rows as a variable name, it's a keyword!

Regards,
Stefi

„Sören_Marodören” ezt *rta:

> Hi,
>
> I use the following line to count rows in a column A
> Rows = Range("A" & Rows.Count).End(xlUp).Row
> (I dont't know if this is the best way, but it works.)
>
> Now I want to have a for loop that goes from column C to the last column
> and count the number of rows in every column.
>
> For Col = 3 to Last_Col
> Rows = ....
> Cells (1, Col) = Rows
> Next
>
> How shall the Row = ... be written to count the number of rows in column Col?
> How can it be written to count columns in another worksheet?
>
>
> Best regards,
> /Sören Nilsson
> Sweden

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      27th Mar 2009
Try this. This will give you the last column/row filled. If you are looking
for values then try with WorkSheetFunction.COUNTIF

For Col = 3 to ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
Cells (1, Col) = ActiveSheet.Cells(Rows.Count, Col).End(xlUp).Row
Next

If this post helps click Yes
---------------
Jacob Skaria


"Sören_Marodören" wrote:

> Hi,
>
> I use the following line to count rows in a column A
> Rows = Range("A" & Rows.Count).End(xlUp).Row
> (I dont't know if this is the best way, but it works.)
>
> Now I want to have a for loop that goes from column C to the last column
> and count the number of rows in every column.
>
> For Col = 3 to Last_Col
> Rows = ....
> Cells (1, Col) = Rows
> Next
>
> How shall the Row = ... be written to count the number of rows in column Col?
> How can it be written to count columns in another worksheet?
>
>
> Best regards,
> /Sören Nilsson
> Sweden

 
Reply With Quote
 
Per Jessen
Guest
Posts: n/a
 
      27th Mar 2009
Hello Sören

Try this:

Sub CountRows()
Dim TargetSh As Worksheet
Dim RowArray()

Set TargetSh = Worksheets("Sheet3")

LastCol = Range("A1").End(xlToRight).Column
ReDim RowArray(3 To LastCol)
For col = 3 To LastCol
RowArray(col) = TargetSh.Cells(Rows.Count, col).End(xlUp).Row
Next
End Sub

Regards,
Per

"Sören_Marodören" <(E-Mail Removed)> skrev i
meddelelsen news:C46D7BB2-9563-4AEB-AA71-(E-Mail Removed)...
> Hi,
>
> I use the following line to count rows in a column A
> Rows = Range("A" & Rows.Count).End(xlUp).Row
> (I dont't know if this is the best way, but it works.)
>
> Now I want to have a for loop that goes from column C to the last column
> and count the number of rows in every column.
>
> For Col = 3 to Last_Col
> Rows = ....
> Cells (1, Col) = Rows
> Next
>
> How shall the Row = ... be written to count the number of rows in column
> Col?
> How can it be written to count columns in another worksheet?
>
>
> Best regards,
> /Sören Nilsson
> Sweden


 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      27th Mar 2009
I see 2 reasons.

1. Out of the below check where you have written your code.

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
'This Workbook event will be triggered for all sheets
End Sub

Private Sub Worksheet_Calculate()
'Each individual sheet has got its own Calculate event which will be
triggered only for that sheet
End Sub

2. Check the sheet reference used in your code. Check whether you have
specified ActiveSheet or Sheets(1) or ...

If this post helps click Yes
---------------
Jacob Skaria


"Sören_Marodören" wrote:

> Hi,
>
> I use the following line to count rows in a column A
> Rows = Range("A" & Rows.Count).End(xlUp).Row
> (I dont't know if this is the best way, but it works.)
>
> Now I want to have a for loop that goes from column C to the last column
> and count the number of rows in every column.
>
> For Col = 3 to Last_Col
> Rows = ....
> Cells (1, Col) = Rows
> Next
>
> How shall the Row = ... be written to count the number of rows in column Col?
> How can it be written to count columns in another worksheet?
>
>
> Best regards,
> /Sören Nilsson
> Sweden

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      27th Mar 2009
Sorry the below is a wrong post....not related to the subject

If this post helps click Yes
---------------
Jacob Skaria


"Jacob Skaria" wrote:

> I see 2 reasons.
>
> 1. Out of the below check where you have written your code.
>
> Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
> 'This Workbook event will be triggered for all sheets
> End Sub
>
> Private Sub Worksheet_Calculate()
> 'Each individual sheet has got its own Calculate event which will be
> triggered only for that sheet
> End Sub
>
> 2. Check the sheet reference used in your code. Check whether you have
> specified ActiveSheet or Sheets(1) or ...
>
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Sören_Marodören" wrote:
>
> > Hi,
> >
> > I use the following line to count rows in a column A
> > Rows = Range("A" & Rows.Count).End(xlUp).Row
> > (I dont't know if this is the best way, but it works.)
> >
> > Now I want to have a for loop that goes from column C to the last column
> > and count the number of rows in every column.
> >
> > For Col = 3 to Last_Col
> > Rows = ....
> > Cells (1, Col) = Rows
> > Next
> >
> > How shall the Row = ... be written to count the number of rows in column Col?
> > How can it be written to count columns in another worksheet?
> >
> >
> > Best regards,
> > /Sören Nilsson
> > Sweden

 
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
Counting rows in a column =?Utf-8?B?TWFy?= Microsoft Excel Misc 2 5th Dec 2005 09:23 PM
Re: Counting the number of items in a column when you hide some rows. Gord Dibben Microsoft Excel Worksheet Functions 3 24th Aug 2004 11:33 PM
Re: Counting the number of items in a column when you hide some rows. Frank Kabel Microsoft Excel Worksheet Functions 0 24th Aug 2004 06:04 PM
help with counting rows with conditions across >1 one column anita Microsoft Excel Worksheet Functions 2 24th Mar 2004 12:26 PM
counting non-empty rows in a column Fede Querio Microsoft Excel Programming 1 19th Jul 2003 05:57 PM


Features
 

Advertising
 

Newsgroups
 


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