+ Reply to Thread
Results 1 to 14 of 14

Sum Lastrow formula!!

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-09-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    259

    Sum Lastrow formula!!

    Hi, I'm not sure where I'm going wrong with this formula? I cannot specify a worksheet as they vary. Thanks

    Sub Sum_Last_Row()
    
    Set Lastrow = Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)
     
    Lastrow = WorksheetFunction.Sum("C7, Lastrow -1")
     
     End Sub
    Last edited by Hurricanefly; 05-17-2013 at 11:57 AM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Sum Lastrow formula!!

    You can only set to an object, such range and sheet, not to a variable.
     Lastrow = Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)

  3. #3
    Forum Contributor
    Join Date
    05-09-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Sum Lastrow formula!!

    Okay, so how would I sum column c? Could I do this using the
    Worksheetfunction.sum
    New to this sorry,

  4. #4
    Forum Contributor
    Join Date
    05-09-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Sum Lastrow formula!!

    The last row in column c. Thanks for any help

  5. #5
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Sum Lastrow formula!!

    Lastrow = Cells(Rows.Count, 3).End(xlUp).Row
     
    Lastrow = Application.WorksheetFunction.Sum(Range("C1:C" & Lastrow))
    I think

  6. #6
    Forum Contributor
    Join Date
    05-09-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Sum Lastrow formula!!

    I get an object fail 1004 with that, but thanks anyway. This works just needs a little formatting then good to go, cheers guys.

    Sub Sum_Last_Row()

    Set Lastrow = Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)
     
    Lastrow.FormulaR1C1 = "=SUM(R2C:R[-1]C)"
     
     
     End Sub

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Sum Lastrow formula!!

    You are still using set with a variable. Remove the set

  8. #8
    Forum Contributor
    Join Date
    05-09-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Sum Lastrow formula!!

    If I remove that I get the fail again!, all I need it to do is sum column c in about 25 tabs in a workbook and it is doing what I want it to do, unless you have any other ideas? Its going to loop through the active workbook and sheets and sum column c. As i'm new this works for me! but open to all suggestions

  9. #9
    Forum Contributor
    Join Date
    05-09-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Sum Lastrow formula!!

    Thanks yudluga I will try it, This will work in all the worksheets in the workbook? well I guess I will find out in a min

  10. #10
    Forum Contributor
    Join Date
    05-09-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Sum Lastrow formula!!

    Yes I'm still getting the "Global fail 1004" With that? I'm not sure why

  11. #11
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Sum Lastrow formula!!

    Are you trying to get the result into the cell at the end of the column C? If so, and you are defining Lastrow as a range object then you are right to use the set and that is also why the code I gave didn't work, you would need:
    Sub Sum_Last_Row()
    Dim Lastrow As Range
    Set Lastrow = Cells(Rows.Count, 3).End(xlUp)
    Lastrow = Application.WorksheetFunction.Sum(Range("C1:C" & Lastrow))
    End Sub

  12. #12
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Sum Lastrow formula!!

    No, just the activesheet, for all sheets I would do that like this:
    Sub sum_C_column()
    Dim wSh
    For Each wSh In ActiveWorkbook.Sheets
    wSh.Range("C" & Cells(Rows.Count, 3).End(xlUp).Row) = Application.WorksheetFunction.Sum(wSh.Range("C1:C" & Cells(Rows.Count, 3).End(xlUp).Row))
    Next
    End Sub
    Edit - I've tested all the code I've put and it all works without error on my setup. Could you upload a workbook with the code as you are getting an error?

  13. #13
    Forum Contributor
    Join Date
    05-09-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Sum Lastrow formula!!

    Okay but let me try your new code yudlugar, appreciate your help, thanks

  14. #14
    Forum Contributor
    Join Date
    05-09-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Sum Lastrow formula!!

    Okay for some reason that doesnt do anything on mine! perhaps I'm doing something wrong, I will upload the workbook, I'm using the code below to sum columns C:J, just need to make some alterations but it seems to work for me


    Sub Sum_Last_Row()
    
    Set Lastrow = Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)
     Lastrow.FormulaR1C1 = "=SUM(R2C:R[-1]C)"
    
    Set Lastrow = Cells(Rows.Count, "D").End(xlUp).Offset(1, 0)
     Lastrow.FormulaR1C1 = "=SUM(R2C:R[-1]C)"
      
    Set Lastrow = Cells(Rows.Count, "E").End(xlUp).Offset(1, 0)
     Lastrow.FormulaR1C1 = "=SUM(R2C:R[-1]C)"
     
    Set Lastrow = Cells(Rows.Count, "F").End(xlUp).Offset(1, 0)
     Lastrow.FormulaR1C1 = "=SUM(R2C:R[-1]C)"
     
    Set Lastrow = Cells(Rows.Count, "G").End(xlUp).Offset(1, 0)
     Lastrow.FormulaR1C1 = "=SUM(R2C:R[-1]C)"
     
    Set Lastrow = Cells(Rows.Count, "H").End(xlUp).Offset(1, 0)
     Lastrow.FormulaR1C1 = "=SUM(R2C:R[-1]C)"
      
    Set Lastrow = Cells(Rows.Count, "I").End(xlUp).Offset(1, 0)
      Lastrow.FormulaR1C1 = "=SUM(R2C:R[-1]C)"
      
    Set Lastrow = Cells(Rows.Count, "J").End(xlUp).Offset(1, 0)
      Lastrow.FormulaR1C1 = "=SUM(R2C:R[-1]C)"
     
     
     
     
     
     
     
     End Sub

    My one is MACRO TEST COPY
    Attached Files Attached Files

+ 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