+ Reply to Thread
Results 1 to 27 of 27

Question in SUMs & SUMIF

Hybrid View

  1. #1
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: Question in SUMs & SUMIF

    If you use Filtering and subtotals you can get the same effect. Pivot tables will do what you ask. Other than that you'll need VBA.

    Something like this (placed in your worksheet's module) should do the trick:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        
        Dim rngIf As Range
        Dim varCriteria As Variant
        Dim rngSum As Range
        
        If Target.Count = 1 Then
            If Left(Target.Formula, 7) = "=SUMIF(" Then
                Set rngIf = Range(Mid(Target.Formula, 8, InStr(1, Target.Formula, ",") - 8))
                If InStr(InStr(1, Target.Formula, ",") + 1, Target.Formula, ",") = 0 Then
                    varCriteria = Replace(Mid(Target.Formula, _
                        InStr(1, Target.Formula, ",") + 1, _
                        InStr(1, Target.Formula, ")") - InStr(1, Target.Formula, ",") - 1), """", "")
                    Set rngSum = Range(Mid(Target.Formula, 8, InStr(1, Target.Formula, ",") - 8))
                Else
                    Set varCriteria = Range(Mid(Target.Formula, _
                        InStr(1, Target.Formula, ",") + 1, _
                        InStr(InStr(1, Target.Formula, ",") + 1, Target.Formula, ",") - InStr(1, Target.Formula, ",") - 1))
                    Set rngSum = Range(Mid(Target.Formula, _
                        InStr(InStr(1, Target.Formula, ",") + 1, Target.Formula, ",") + 1, _
                        InStr(1, Target.Formula, ")") - InStr(InStr(1, Target.Formula, ",") + 1, Target.Formula, ",") - 1))
                End If
                Call ShowValues(rngIf, varCriteria, rngSum, Target)
            End If
        End If
    End Sub
    
    Private Sub ShowValues(rngIf As Range, varCriteria As Variant, rngSum As Range, rngTarget As Range)
    
        Dim Result As String
        Dim Operator As String
        Dim Operators As Variant
        Dim i As Long
        
        Operator = "="
        Operators = Array(">=", "<=", "<>", "<", ">")
        
        For i = 0 To 4
            If InStr(1, varCriteria, Operators(i)) Then
                Operator = Operators(i)
                varCriteria = Replace(varCriteria, Operator, "")
            End If
        Next i
        
        varCriteria = Replace(varCriteria, "&", "")
        
        If Not IsError(Application.Evaluate(varCriteria)) Then
            varCriteria = Application.Evaluate(varCriteria)
        End If
        
        For i = 1 To rngIf.Count
            If Operator <> "=" Then
                If Application.Evaluate(rngIf(i, 1).Value & Operator & varCriteria) Then
                    Result = Result & Application.Rept(" ", Len(Format(rngTarget.Value, "#,##0.00")) + 2 - Len(Format(rngSum(i, 1).Value, "#,##0.00"))) & Format(rngSum(i, 1).Value, "#,##0.00") & Chr(10) & Chr(13)
                End If
            Else
                If rngIf(i, 1).Value = varCriteria Then
                    Result = Result & Application.Rept(" ", Len(Format(rngTarget.Value, "#,##0.00")) + 2 - Len(Format(rngSum(i, 1).Value, "#,##0.00"))) & Format(rngSum(i, 1).Value, "#,##0.00") & Chr(10) & Chr(13)
                End If
            End If
        Next i
        
        Result = Result & Application.Rept("-", Len(Format(rngTarget.Value, "#,##0.00")) + 2) & Chr(10) & Chr(13) & Application.Rept(" ", 2) & Format(rngTarget.Value, "#,###0.00")
        
        MsgBox Result
    
    End Sub
    Note that if your SUMIF function is pointing to an entire column, it may be slightly slow to calculate. Also note that this really only works for a simple SUMIF formula. If you have SUMIF nested inside another function, or another function nested inside SUMIF, then it will not work.

    [Edit]
    Oh, also note that the SUMRANGE part of SUMIF is no longer optional with the above. I'll make another version where it is optional and edit this post. Hang on.

    [Edit the second]
    Ok, updated the script for the SUMRANGE to be optional. Also, the CRITERIA parameter can now be a string. There are probably better ways to do this, but there it is.

    See the attached for an example.
    Attached Files Attached Files
    Last edited by Whizbang; 04-30-2015 at 01:02 PM.

  2. #2
    Registered User
    Join Date
    04-29-2015
    Location
    Doha, Qatar
    MS-Off Ver
    2007
    Posts
    29

    Re: Question in SUMs & SUMIF

    @ Whizbang

    You have no single clue how much of a help you have been.

    This is great !

    This is EPIC !

    This is making my life so easy !!!

    Thanks a lot Bro.

    So let me thank you.

    =THANKYOU*1000000



    Just help me out how should I now add this to my sheet to get it working?

    Like what should be looking at when I do the changes.

    Thank you ! Thank you ! Thank you !

    P.S - In case I add sheets or remove any sheet without the Sheet where VBA is written to, can it stop working by any chance, etc, should I have to be cautious of?

+ 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 - Sumif question
    By Andrewbutler in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-24-2014, 01:16 PM
  2. [SOLVED] SUMIF sums criteria it shouldn't
    By Baron J79 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-17-2013, 07:39 PM
  3. if(sumif(or....question
    By jw01 in forum Excel General
    Replies: 4
    Last Post: 01-24-2012, 02:27 PM
  4. SUMIF Question: SUMIF not blank
    By nickyg in forum Excel General
    Replies: 5
    Last Post: 11-18-2009, 10:07 PM
  5. Sums question...
    By Ken Spiker in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 10-01-2007, 01:55 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