+ Reply to Thread
Results 1 to 11 of 11

making sumifs work in VBA

  1. #1
    Registered User
    Join Date
    02-11-2014
    Location
    Central New York
    MS-Off Ver
    Excel 2010
    Posts
    18

    making sumifs work in VBA

    Here is my code:

    Please Login or Register  to view this content.
    Everything else work accept the red row. I am getting Run time error 13, type mismatch.

    Can anyone tell me where I am going wrong?

    Thanks so much,

    Angelina

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: making sumifs work in VBA

    SUMIFS take arguments in this order

    RangeToSum, Criteria_Range1, Condition 1, Criteria_Range 2, Condition 2

    So...
    sheet.Range("A:A"), ("A:A") + WorksheetFunction.SumIfs(sheet.Range("E:E")
    is a valid range but not a valid condition combination....

    Get the formula to work on a sheet, then you will have a basis to start from...
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    02-11-2014
    Location
    Central New York
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: making sumifs work in VBA

    Please Login or Register  to view this content.
    This is the current formula on the sheet. It works well....Something got lost in translation lol

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: making sumifs work in VBA

    =SUMIFS(Data!E:E,Data!D:D,"D",Data!A:A,A:A)+SUMIFS(Data!E:E,Data!D:D,"R",Data!A:A,A:A)

    This kind of construct - Data!A:A,A:A - in a formula on a sheet picks up the value from column A of the same row in place of the A:A. That won't work in VBA, since you are not placing the formula in a cell - not sure which A you want to refer to - assuming sheet:


    ws.Cells(iRow, 4).Value = Application.WorksheetFunction.SumIfs(sheet.Range("E:E"), sheet.Range("D:D"), "D", sheet.Range("A:A"), sheet.Range("A" & iRow).Value) + Application.WorksheetFunction.SumIfs(sheet.Range("E:E"), sheet.Range("D:D"), "R", sheet.Range("A:A"), sheet.Range("A" & iRow).Value)

    or possibly (maybe even probably) ws

    ws.Cells(iRow, 4).Value = Application.WorksheetFunction.SumIfs(sheet.Range("E:E"), sheet.Range("D:D"), "D", sheet.Range("A:A"), ws.Range("A" & iRow).Value) + Application.WorksheetFunction.SumIfs(sheet.Range("E:E"), sheet.Range("D:D"), "R", sheet.Range("A:A"), ws.Range("A" & iRow).Value)

  5. #5
    Registered User
    Join Date
    02-11-2014
    Location
    Central New York
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: making sumifs work in VBA

    thanks Bernie,

    I tried both. I no longer have the error message so thanks for that, but the cell is not adding as it should. I sending a link for a cut down workbook for you to see. My form should add an new employee row on the "totals" sheet and the formula that I am trying to make work should go in column D of the new row.

    Then you should be able to add points from the forms "Add points" tab and you should see it added in the "observer/column D".


    https://onedrive.live.com/edit.aspx?...1107&app=Excel

    Thanks again,

    Angelina

  6. #6
    Registered User
    Join Date
    02-11-2014
    Location
    Central New York
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: making sumifs work in VBA

    It occurred to me that you might not have a Microsoft account so I tried to attaché a copy of the workbook....however, the uploader was taking an extremely long time....

  7. #7
    Registered User
    Join Date
    02-11-2014
    Location
    Central New York
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: making sumifs work in VBA

    Its funny, I tried out your second formula and it sort of works. If I enter an new employee on the "totals" sheet, then the formula calculates as "0" which it should. However once I add points into the "data" sheet the formula should calculate them but it doesn't. So then I go and delete the new employee that I added but not the points and re-add on the "totals" sheet and the formula calculates correctly. But when I go and delete points off the "Data" sheet the points are still there....how weird.

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: making sumifs work in VBA

    Well, that is because you are adding the value that is calculated by the formula, and not the formula itself. Try changing the

    ws.Cells(iRow, 4).Value

    to

    ws.Cells(iRow, 4).Formula = "=SUMIFS(Data!E:E,Data!D:D,"D",Data!A:A,A:A)+SUMIFS(Data!E:E,Data!D:D,"R",Data!A:A,A:A)"

    That should add the formula that was working for you....

  9. #9
    Registered User
    Join Date
    02-11-2014
    Location
    Central New York
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: making sumifs work in VBA

    this gives me a sytex error

    I decided to record a macro to see what came up....

    this is the working code:

    Please Login or Register  to view this content.
    I'm not sure what all this means, but it works so I'll take it!!

    Thanks so much Bernie for trying to help.

    A~

  10. #10
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: making sumifs work in VBA

    Sorry - I forgot to double the "s

    Please Login or Register  to view this content.
    It is often easier to work with the Formula rather than the FormulaR1C1 since it looks more familiar, and it doesn't need to change if you change what column/row/cell you are putting it into.

  11. #11
    Registered User
    Join Date
    02-11-2014
    Location
    Central New York
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: making sumifs work in VBA

    That works well too, thanks for the tip!!

    Have a fantastic day!!

+ 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. SUMIFS - Can't get it to work
    By wmjenner in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-09-2013, 03:37 PM
  2. [SOLVED] Cannot get INDIRECT function to work with SUMIFS
    By kborgers in forum Excel General
    Replies: 6
    Last Post: 11-22-2012, 11:21 AM
  3. Is it SUMIFS can work here if yes how?
    By Naghman in forum Excel General
    Replies: 1
    Last Post: 04-06-2011, 03:51 PM
  4. SUMIFS on Multiple Work Sheets
    By SeaTiger in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-01-2011, 04:56 AM
  5. SUMIFS won't work with >= as a criteria
    By JYonker in forum Excel General
    Replies: 4
    Last Post: 11-19-2010, 06:12 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