+ Reply to Thread
Results 1 to 8 of 8

Sumif function returning 0

  1. #1
    Registered User
    Join Date
    05-17-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    4

    Sumif function returning 0

    Hi,

    I'm new to VBA and having some difficulty getting a sumif to work within a larger macro. Currently it is returning zeros in column I.

    I'm trying to loop this part of the macro so that it runs through a range of criteria one at a time (in column H), identifies matching criteria in column E, and returns the sum of the cells from column C corresponding to column E. I want to place the summed values in Column I, next to their corresponding criteria in column H.

    In the below code:
    RangeofCriteria = the number of rows in the list of criteria. I would like to essentially conduct a sumif for each of these criteria, hence the loop.

    TopRowofCriteria = the row number for the first row in column H for which I would like to conduct a sumif - also the first row where I would like to place the first value in Column I

    TopRowofSumRange = the row number for the top row of my matching criteria, also for the sum range
    EndRowofSumRange = the row number for the last row of my matching criteria, also for the sum range

    Here is the code:

    For i = 0 To RangeofCriteria
    Cells(TopRowofCriteria + i, "I").Value = Application.WorksheetFunction.sumif(Sheets("Timelog").Range("C" & TopRowofSumRange, "C" & EndRowofSumRange), Sheets("Timelog").Cells(TopRowofCriteria + i, "H").Value, Sheets("Timelog").Range("E" & TopRowofSumRange, "E" & EndRowofSumRange))
    Next i

    I would really appreciate any help.

    Thanks.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: Sumif function returning 0

    Hi, welcome to the forum

    I want to place the summed values in Column I, next to their corresponding criteria in column H.
    Then why not just use a regular SUMIF() formula in those cells, instead of VBA?
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    05-17-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    4

    Re: Sumif function returning 0

    Hi,

    Thanks for the welcome!

    This is part of a larger macro where I am doing a few different things to the data. I want the macro to flow right through rather than having it stop and restart.

    Also, this a bit of a learning exercise for me so it's important that I understand what I am doing wrong within the syntax.

    Any ideas?

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Sumif function returning 0

    ran this thru with test data

    it works fine

    Please Login or Register  to view this content.
    your problem may lie with your variables
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  5. #5
    Registered User
    Join Date
    05-17-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    4

    Re: Sumif function returning 0

    I tested my variables using the code below to check they were the ranges I expected.

    I didn't find any issues, so I'm not sure the problem is with my variables.


    Cells(TopRowofCriteria + i, "I").Select
    Range("C" & TopRowofSumRange, "C" & EndRowofSumRange).Select
    Sheets("Timelog").Cells(TopRowofCriteria + i, "H").Select
    Sheets("Timelog").Range("E" & TopRowofSumRange, "E" & EndRowofSumRange).Select

    Any other ideas?

  6. #6
    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,425

    Re: Sumif function returning 0

    Any other ideas?
    Yep, paste a sample workbook.

    And please take moment to (re)read the forum rules.

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    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


  7. #7
    Registered User
    Join Date
    05-17-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    4

    Re: Sumif function returning 0

    Thanks for the help all.

    I solved the issue using an If statement.

    Please Login or Register  to view this content.

  8. #8
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: Sumif function returning 0

    Thanks for sharing the answer with us

+ 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] SUMIF not returning what I'd expect
    By PFDave in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-13-2016, 07:10 AM
  2. SUMIF returning 0 (formatting?)
    By Yakov on Excel in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 04-01-2016, 04:13 PM
  3. [SOLVED] SUMIF returning 0
    By TPDave in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 04-25-2014, 11:38 AM
  4. [SOLVED] SUMIF formula returning a 0 value
    By cf7046 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 04-21-2013, 01:08 AM
  5. Sumif function not returning text
    By slkelly13 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-02-2013, 06:01 PM
  6. SUMIF or IF without returning to false or zero value
    By tmkkoservo in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-03-2012, 09:23 PM
  7. Need function help on returning a value from lookup or Sumif
    By spanky27 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-02-2008, 09:20 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