+ Reply to Thread
Results 1 to 14 of 14

EXCEL 2003 Macro to get sum

  1. #1
    Registered User
    Join Date
    02-03-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    18

    EXCEL 2003 Macro to get sum

    Hi,

    I am new to macro,

    Can anyone tell me what's wrong with the code below?
    I need to get sum for every week.ie, this week is sum for week1 to week5, next time it will be week 1 to week6.
    However the last line of the code doesn't give me the sum.

    Dim m_copycell As String
    Sheets("SHEETA").Select
    Range("L5").Select
    m_copycell = Range("L5").Value
    Range("F4").Select
    Cells("F4").Formula = "=SUM(B2:m_coypcell")"

    Thanks a lot.

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: EXCEL 2003 Macro to get sum

    The first problem is that your code should be in code tags

    Secondly, because the value m_copycell is inside the quotes on the last line it will be treated as a literal value. If you want it to be used as a variable you need:

    Please Login or Register  to view this content.
    But that will only work if m_copycell contains a valid cell reference.

    Thirdly, lines 2, 3 and 5 of your code don't do anything useful and should be deleted.

  3. #3
    Registered User
    Join Date
    02-03-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: EXCEL 2003 Macro to get sum

    Quote Originally Posted by Andrew-R View Post
    The first problem is that your code should be in code tags

    Secondly, because the value m_copycell is inside the quotes on the last line it will be treated as a literal value. If you want it to be used as a variable you need:

    Please Login or Register  to view this content.
    But that will only work if m_copycell contains a valid cell reference.

    Thirdly, lines 2, 3 and 5 of your code don't do anything useful and should be deleted.

    Thank you for the quick reply, I tried your code,but it still not working, it give me an error message say " type mismatch".
    m_copycell is equal to one cell, the idea is every week I just type in up to what line the sum total should be. ie. line B6 is week5, so next week, I will change the value in that cell to B7 to get another week's total.

  4. #4
    Registered User
    Join Date
    02-03-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: EXCEL 2003 Macro to get sum

    Thank you for the quick reply, I tried your code,but it still not working, it give me an error message say " type mismatch".
    m_copycell is equal to one cell, the idea is every week I just type in up to what line the sum total should be. ie. line B6 is week5, so next week, I will change the value in that cell to B7 to get another week's total.

  5. #5
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: EXCEL 2003 Macro to get sum

    So does cell L5 contain the value "B6", or "5" or "Week 5"?

  6. #6
    Registered User
    Join Date
    02-03-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: EXCEL 2003 Macro to get sum

    Yes, it is "B6"

  7. #7
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: EXCEL 2003 Macro to get sum

    D'Oh, sorry, I missed the obvious - you can't use a string reference with Cells.

    Change that final line to:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    02-03-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: EXCEL 2003 Macro to get sum

    Hi Andrew_R, I tried this code, but it still not working for me, it said" Application-defined of object defined error thank you

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: EXCEL 2003 Macro to get sum

    Setting the range that way works for me. Try this:

    Please Login or Register  to view this content.

  10. #10
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: EXCEL 2003 Macro to get sum

    Note the corrected spelling
    Please Login or Register  to view this content.
    Good luck.

  11. #11
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: EXCEL 2003 Macro to get sum

    Um, yeah, that would explain it. It's always the simple stuff, isn't it?

  12. #12
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: EXCEL 2003 Macro to get sum

    Oh, and while I think on, you do realise that this whole thing could be done in a formula in cell F4, don't you:

    =SUM(INDIRECT("B2:" & L5))

  13. #13
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: EXCEL 2003 Macro to get sum

    Quote Originally Posted by Andrew-R View Post
    It's always the simple stuff, isn't it?
    That is the only stuff I understand.

  14. #14
    Registered User
    Join Date
    02-03-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: EXCEL 2003 Macro to get sum

    Thanks everyone, it works, either way works, it was a typo. Happy days.

+ 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