+ Reply to Thread
Results 1 to 4 of 4

Macro works when there are multiple rows but errors with one or no rows

  1. #1
    Registered User
    Join Date
    01-04-2014
    Location
    Toronto
    MS-Off Ver
    Excel 2007
    Posts
    10

    Macro works when there are multiple rows but errors with one or no rows

    I created this Macro to add a sum and count after the last row of data. However when the sheet has only one row of data or only a header row I receive an error. I won't know which sheets have data and or multiple rows, so would like something that would work in all instances.

    Sheets("Sheet1").Select
    Dim NRow As Long
    NRow = Range("S2").End(xlDown).Row
    Cells(NRow + 1, "S").Formula = "=SUM(S2:S" & LastRow & ")"
    Dim ORow As Long
    ORow = Range("A2").End(xlDown).Row
    Cells(ORow + 1, "A").Formula = "=Count(A2:A" & LastRow & ")"

    Any ideas would be appreciated
    Last edited by santanicopandimonium; 06-10-2019 at 11:19 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2003,2007,2010
    Posts
    29,249

    Re: Macro works when there are multiple rows but errors with one or no rows

    Depending on the data layout, I would normally use:

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Excel Aid

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    01-04-2014
    Location
    Toronto
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macro works when there are multiple rows but errors with one or no rows

    This is perfect!
    Thank you so much

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2003,2007,2010
    Posts
    29,249

    Re: Macro works when there are multiple rows but errors with one or no rows

    You're welcome. Thanks for the rep.

+ 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