+ 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
    15

    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 2007,2010,365
    Posts
    44,086

    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 - Retired Excel/VBA Consultant

    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
    15

    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 2007,2010,365
    Posts
    44,086

    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)

Similar Threads

  1. [SOLVED] Delete Rows (w/Zero & Errors) but Not My Notes
    By delaing in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-04-2016, 07:29 PM
  2. [SOLVED] Apply forluma on formulated rows
    By Sai Prashanth in forum Excel General
    Replies: 10
    Last Post: 09-26-2014, 03:43 PM
  3. Delete all rows with #REF errors
    By adgjqetuo in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-19-2013, 01:57 PM
  4. Multiple ccolums/rows to get data from multiple columns/rows (vlookup)
    By Ramzes in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-18-2010, 05:35 AM
  5. Replies: 1
    Last Post: 05-18-2009, 11:21 AM
  6. Replies: 1
    Last Post: 03-18-2009, 04:18 PM
  7. #REF! errors due to number of rows
    By sb in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 03-02-2006, 03:10 PM

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