+ Reply to Thread
Results 1 to 11 of 11

sum funtion not working in VBA

  1. #1
    Registered User
    Join Date
    08-19-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    27

    sum funtion not working in VBA

    Good Morning,
    I would appreciate some help with troubleshooting my formula in vba

    I am trying to sum a range of cells as follows:

    lastYrOSHA = Application.WorksheetFunction.Sum(Cells(39, 10), Cells(eRow, 10))

    where eRow is the last row which is calculated using another formula (erow = 32 currently)

    the sum formula is returning a value of 1 and it should be 8

    If I use the exact cell reference in a select Range formula it selects the range fine

    Range(Cells(39, 10), Cells(eRow, 10)).Select

    Why is it returning a value of 1 when the sum of these cells is 8

    Thank you,

    Mike

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: sum funtion not working in VBA

    Are all the cells you're trying to add formatted as a number?

    can you attached your workbook for us to look over?
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: sum funtion not working in VBA

    Hi, and welcome to the forum,

    (Cells(39, 10), Cells(eRow, 10))

    evaluates to J32:J39

    What is the sum of this range?
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    08-19-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: sum funtion not working in VBA

    Richard,

    The sum of this range should be 8, but I am only returning a 1

    Mike

  5. #5
    Registered User
    Join Date
    08-19-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: sum funtion not working in VBA

    Xero,

    sorry if this post twice, but I did not see it the first time (newby).

    My cellse look as follows:
    J32=1
    J33=2
    J34 =0
    J35=0
    J36 = 3
    J37 =2
    J38 =0
    J39 =0

    A numbers in this range are formated to be numbers with no decimal points.

    Mike

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

    Re: sum funtion not working in VBA

    If you are using erow as 32 and this is the lastrow, then I think your function will just return the value of cell J32 as cells J33 to 39 are going to be empty...

    When I test it, if I put 8 in J32, your sum function returns 8.

    I'd suggest uploading a workbook as I'm not sure your macro is doing what you think/say it is.

    Edit: just saw the new info, think you can ignore this post, I'd still guess that the erow formula is the problem.
    Last edited by ragulduy; 08-19-2013 at 09:23 AM.

  7. #7
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: sum funtion not working in VBA

    That is only evaluating the two rows (32 & 39). Try:
    Please Login or Register  to view this content.

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: sum funtion not working in VBA

    Hello Mike,

    Those might be the 'numbers' in the range, but what is the sum of them? Test with a normal =SUM(J32:J39)

    I ask because perhaps all except J32 are in fact text 'numbers'. Formatting is irrelevant, it's what the cell values are that's important.

    However try

    Application.WorksheetFunction.Sum(Range(Cells(39, 10), Cells(erow, 10)))
    Last edited by Richard Buttrey; 08-19-2013 at 09:28 AM.

  9. #9
    Registered User
    Join Date
    08-19-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: sum funtion not working in VBA

    Yudlugar,

    You are correct, I just change J32 to be a 5 and my formula returened a sum of 5, hmmm.... Does this mean that my range in this sum formula is only looking at J32?? That is confusing as when I use it in a range formual it selects the entire range.

    Thanks for the catch,

    Mike

  10. #10
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: sum funtion not working in VBA

    Or:
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    08-19-2013
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: sum funtion not working in VBA

    Thank you all, Xero solved the problem. I can take deep breath now

+ 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. Funtion Help
    By snu2216 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-21-2011, 01:38 PM
  2. copy paste funtion with Thisworkbook and ActiveSheet not working?
    By trillium in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-25-2011, 04:02 PM
  3. Why Isnt My If funtion Working When comparing ROI
    By Dberends in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-11-2009, 10:50 AM
  4. help with if funtion
    By serijuro in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-08-2007, 07:55 PM

Tags for this Thread

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