+ Reply to Thread
Results 1 to 5 of 5

Counting rows in Excel spreadsheet.

  1. #1
    Forum Contributor
    Join Date
    08-15-2005
    Location
    Oklahoma
    MS-Off Ver
    2010, 2013
    Posts
    112

    Counting rows in Excel spreadsheet.

    I recorded the following macro to calculate data in rows. However, the spreadsheet I was working with had 421 rows. Is there something that will replace the following lines to only process the number of rows in a spreadsheet that are present?

    Selection.AutoFill Destination:=Range("I2:I421")
    Range("I2:I421").Select
    Cells.Select


    Thanks,

    Jim

    Sub Prod_Abstract()
    '
    ' Prod_Abstract Macro
    ' Macro recorded 3/9/2006 by JBW
    '
    Columns("I:I").Select
    Selection.Insert Shift:=xlToRight
    Range("I1").Select
    ActiveCell.FormulaR1C1 = "MMCFE"
    Range("I2").Select
    ActiveCell.FormulaR1C1 = "=SUM((RC[-2]*6+RC[-1])/1000)"
    Selection.Copy
    Application.CutCopyMode = False
    Selection.AutoFill Destination:=Range("I2:I421")
    Range("I2:I421").Select
    Cells.Select
    Selection.Sort Key1:=Range("I2"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    Columns("G:I").Select
    Selection.NumberFormat = "#,##0"
    End Sub

  2. #2
    Tom Ogilvy
    Guest

    RE: Counting rows in Excel spreadsheet.

    Assume we can figure out the number of rows by looking a column A (adjust to
    point to the column that will work).

    Dim lastrow as Long
    lastrow = cells(rows.count,"A").End(xlup).row
    Selection.AutoFill Destination:=Range("I2:I" & lastrow)
    Range("I2:I" & lastrow).Select

    If you want to use

    Cells.select
    then you don't need
    Range("I2:I" & lastrow).Select

    --
    Regards,
    Tom Ogilvy




    "Jim15" wrote:

    >
    > I recorded the following macro to calculate data in rows. However, the
    > spreadsheet I was working with had 421 rows. Is there something that
    > will replace the following lines to only process the number of rows in
    > a spreadsheet that are present?
    >
    > Selection.AutoFill Destination:=Range("I2:I421")
    > Range("I2:I421").Select
    > Cells.Select
    >
    >
    > Thanks,
    >
    > Jim
    >
    > Sub Prod_Abstract()
    > '
    > ' Prod_Abstract Macro
    > ' Macro recorded 3/9/2006 by JBW
    > '
    > Columns("I:I").Select
    > Selection.Insert Shift:=xlToRight
    > Range("I1").Select
    > ActiveCell.FormulaR1C1 = "MMCFE"
    > Range("I2").Select
    > ActiveCell.FormulaR1C1 = "=SUM((RC[-2]*6+RC[-1])/1000)"
    > Selection.Copy
    > Application.CutCopyMode = False
    > Selection.AutoFill Destination:=Range("I2:I421")
    > Range("I2:I421").Select
    > Cells.Select
    > Selection.Sort Key1:=Range("I2"), Order1:=xlAscending,
    > Header:=xlGuess, _
    > OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
    > _
    > DataOption1:=xlSortNormal
    > Columns("G:I").Select
    > Selection.NumberFormat = "#,##0"
    > End Sub
    >
    >
    > --
    > Jim15
    > ------------------------------------------------------------------------
    > Jim15's Profile: http://www.excelforum.com/member.php...o&userid=26300
    > View this thread: http://www.excelforum.com/showthread...hreadid=547911
    >
    >


  3. #3
    Forum Contributor
    Join Date
    08-15-2005
    Location
    Oklahoma
    MS-Off Ver
    2010, 2013
    Posts
    112

    Works great but does not sort in ascending order.

    Thanks for the solution but the sort at the end of the macro no longer works.

    Here is the code.

    Sub Prod_Abstract()
    '
    ' Prod_Abstract Macro
    ' Macro recorded 3/9/2006 by JBW
    '
    Dim lastrow As Long
    Columns("I:I").Select
    Selection.Insert Shift:=xlToRight
    Range("I1").Select
    ActiveCell.FormulaR1C1 = "MMCFE"
    Range("I2").Select
    ActiveCell.FormulaR1C1 = "=SUM((RC[-2]*6+RC[-1])/1000)"
    Selection.Copy
    Application.CutCopyMode = False
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    Selection.AutoFill Destination:=Range("I2:I" & lastrow)
    Range("I2:I" & lastrow).Select
    Selection.Sort Key1:=Range("I2"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    Columns("G:I").Select
    Selection.NumberFormat = "#,##0"
    End Sub

  4. #4
    Tom Ogilvy
    Guest

    Re: Counting rows in Excel spreadsheet.

    I said you don't need to do both selects. In this case, you need to use the
    cells.select

    Sub Prod_Abstract()
    '
    ' Prod_Abstract Macro
    ' Macro recorded 3/9/2006 by JBW
    '
    Dim lastrow As Long
    Columns("I:I").Select
    Selection.Insert Shift:=xlToRight
    Range("I1").Select
    ActiveCell.FormulaR1C1 = "MMCFE"
    Range("I2").Select
    ActiveCell.FormulaR1C1 = "=SUM((RC[-2]*6+RC[-1])/1000)"
    Selection.Copy
    Application.CutCopyMode = False
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    Selection.AutoFill Destination:=Range("I2:I" & lastrow)
    Cells.Select
    Selection.Sort Key1:=Range("I2"), Order1:=xlAscending,
    Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    Columns("G:I").Select
    Selection.NumberFormat = "#,##0"
    End Sub

    --
    Regards,
    Tom Ogilvy

    "Jim15" wrote:

    >
    > Thanks for the solution but the sort at the end of the macro no longer
    > works.
    >
    > Here is the code.
    >
    > Sub Prod_Abstract()
    > '
    > ' Prod_Abstract Macro
    > ' Macro recorded 3/9/2006 by JBW
    > '
    > Dim lastrow As Long
    > Columns("I:I").Select
    > Selection.Insert Shift:=xlToRight
    > Range("I1").Select
    > ActiveCell.FormulaR1C1 = "MMCFE"
    > Range("I2").Select
    > ActiveCell.FormulaR1C1 = "=SUM((RC[-2]*6+RC[-1])/1000)"
    > Selection.Copy
    > Application.CutCopyMode = False
    > lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    > Selection.AutoFill Destination:=Range("I2:I" & lastrow)
    > Range("I2:I" & lastrow).Select
    > Selection.Sort Key1:=Range("I2"), Order1:=xlAscending,
    > Header:=xlGuess, _
    > OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
    > _
    > DataOption1:=xlSortNormal
    > Columns("G:I").Select
    > Selection.NumberFormat = "#,##0"
    > End Sub
    >
    >
    > --
    > Jim15
    > ------------------------------------------------------------------------
    > Jim15's Profile: http://www.excelforum.com/member.php...o&userid=26300
    > View this thread: http://www.excelforum.com/showthread...hreadid=547911
    >
    >


  5. #5
    Forum Contributor
    Join Date
    08-15-2005
    Location
    Oklahoma
    MS-Off Ver
    2010, 2013
    Posts
    112

    Works now.

    Thanks Mr. Ogilvy!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1