+ Reply to Thread
Results 1 to 5 of 5

SumIfs function in VBA with Date as one criteria

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    SumIfs function in VBA with Date as one criteria

    I'm trying to use the SumIfs function from VBA but I'm having trouble with the date criteria.

    Background:
    Computer Regional Settings date format: dd/MM/yyyy
    Excel displays date as: dd/MM/yyyy in the formula bar
    dVariable is Dimmed as Date

    Sum Range is BD6:BD11006
    Criteria 1 Range is D6:D11006 (the above mentioned dates in this column)
    dVariable contains date 28/12/2005
    Criteria 2 Range is E6:E11006 (this is a text column)
    cVariable contains string ABC-DE

    I want to use the SumIfs function to sum all the values in range BD6:BD11006 where the date in range D6:D11006 <= dVariable (28/12/2005) and the string in range E6:E11006 = cVariable (ABC-DE) but the date part is not working. If I leave the date part out the function returns the correct answer but when I add the date criteria the function returns 0.

    I suspect the problem is the date format (might be wrong!) but can't figure out what to do.

    'the ranges used below is ranges in the code; just entered them here as variables for the example sake
    
    nTotal = Application.WorksheetFunction.SumIfs(cSumRange, cCrit1Range, "<=" & dVariable, cCrit2Range, "=" & cVariable)
    Any ideas how to do this or how to get the function to convert the date into whatever is needed?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: SumIfs function in VBA with Date as one criteria

    How have you declared dVariable and cVariable?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: SumIfs function in VBA with Date as one criteria

    Dim cVariable As String
    Dim dVariable As Date

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: SumIfs function in VBA with Date as one criteria

    Does this work?
    Formula: copy to clipboard

    nTotal = Application.WorksheetFunction.SumIfs(cSumRange, cCrit1Range, "<=" & CLng(dVariable), cCrit2Range, "=" & cVariable)

  5. #5
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: SumIfs function in VBA with Date as one criteria

    Thanks. Looks like it works. Will need to test some more but so far it's looking good.

+ 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/SUMIFS not working using Date fields as criteria
    By waynees in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-08-2013, 09:24 AM
  2. Sum rage = criteria range in sumifs function
    By mj1118888 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-15-2013, 12:25 PM
  3. SUMIFS w/ multiple criteria and an or function
    By jacobkmc in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-12-2013, 09:22 PM
  4. SUMIFS function with calculated criteria
    By RollsWRangler in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-24-2013, 11:35 AM
  5. [SOLVED] Use TEXT function as Criteria on Sumifs to convert month/year to date format
    By chico.corrales in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-08-2012, 07:49 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