+ Reply to Thread
Results 1 to 10 of 10

Convert Sumifs to vba code with loop

Hybrid View

  1. #1
    Registered User
    Join Date
    11-11-2015
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    3

    Convert Sumifs to vba code with loop

    I need to convert:

    Formula: copy to clipboard
    =SUMIFS($D:$D;$A:$A;"="&$G2;$B:$B;$H2;$C:$C;"="&$N$1)  


    to VBA code and find a way to loop it so I can fill column K (based on the length of column G) of the example with the correct sums.

    sumifexample.xlsx

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Convert Sumifs to vba code with loop

    You have prepared col G & H, Criteria 1 & 2.
    Do you want to keep it remain as it is, or is it fine just the data that have month=N1?

    This is to output the only data that have Month = N1
    to a Sheet Module
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address(0, 0) <> "N1" Then Exit Sub
        Application.EnableEvents = False
        test
        Application.EnableEvents = True
    End Sub
    To a standard module
    Sub test()
        Dim a, i As Long, ii As Long, w, txt As String, myMonth As Long
        myMonth = [n1].Value
        a = Cells(1).CurrentRegion.Resize(, 4).Value
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            For i = 2 To UBound(a, 1)
                If a(i, 3) = myMonth Then
                    txt = Join(Array(a(i, 1), a(i, 2)), Chr(2))
                    If Not .exists(txt) Then
                        ReDim w(1 To 3)
                        For ii = 1 To 2
                            w(ii) = a(i, ii)
                        Next
                    Else
                        w = .Item(txt)
                    End If
                    w(3) = w(3) + a(i, 4)
                    .Item(txt) = w
                End If
            Next
            a = Empty
            If .Count Then a = Application.Index(.items, 0, 0)
        End With
        [g1:h1].CurrentRegion.Offset(1).ClearContents
        [k1].CurrentRegion.Offset(1).ClearContents
        If IsArray(a) Then
            [g2:h2].Resize(UBound(a, 1)).Value = a
            [k2].Resize(UBound(a, 1)).Value = Application.Index(a, 0, 3)
        End If
    End Sub
    Attached Files Attached Files
    Last edited by jindon; 11-11-2015 at 07:43 PM.

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Convert Sumifs to vba code with loop

    Hi lakamas,

    Welcome to the Forum - here's another routine:

    Sub lakamas(): Dim M As Long, Criteria As String, r As Long, K, Z, s As Long
                Columns("G:H").CurrentRegion.Offset(1, 0).ClearContents
                Columns("K").CurrentRegion.Offset(1, 0).ClearContents
        s = 2: M = Cells(1, "N"): With CreateObject("Scripting.Dictionary")
                    For r = 2 To Range("A" & Rows.count).End(xlUp).Row
                    Criteria = Trim(Range("A" & r)) & "|" & _
                    Trim(Range("B" & r)) & "|" & Val(Range("C" & r))
                    If .exists(Criteria) Then
                    .Item(Criteria) = .Item(Criteria) + Val(Range("D" & r))
                    Else: .Item(Criteria) = Val(Range("D" & r))
                    End If: Next r: K = .Keys()
                            For r = LBound(K) To UBound(K): Z = Split(K(r), "|")
                            If Z(UBound(Z)) = M Then
                                Range("G" & s) = Z(0): Range("H" & s) = Z(1)
                                Range("K" & s) = .Item(K(r)): s = s + 1: End If
                            Next r: End With: End Sub
    Last edited by xladept; 11-11-2015 at 07:45 PM. Reason: Clear K
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Convert Sumifs to vba code with loop

    This is closer to what you said:

    Sub lakamasII(): Dim M As Long, Criteria As String, r As Long
                Columns("K").CurrentRegion.Offset(1, 0).ClearContents
             M = Cells(1, "N"): With CreateObject("Scripting.Dictionary")
                    For r = 2 To Range("A" & Rows.count).End(xlUp).Row
                    Criteria = Trim(Range("A" & r)) & "|" & _
                    Trim(Range("B" & r)) & "|" & Val(Range("C" & r))
                    If .Exists(Criteria) Then
                    .Item(Criteria) = .Item(Criteria) + Val(Range("D" & r))
                    Else: .Item(Criteria) = Val(Range("D" & r))
                    End If: Next r
                            For r = 2 To Range("G" & Rows.count).End(xlUp).Row
                            Criteria = Trim(Range("G" & r)) & "|" & _
                                        Trim(Range("H" & r)) & "|" & M
                            If .Exists(Criteria) Then _
                                Range("K" & r) = .Item(Criteria) Else Range("K" & r) = 0
                            Next r: End With: End Sub

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

    Re: Convert Sumifs to vba code with loop

    Perhaps.
    Range("K2:K" & Range("G" & Rows.Count).End(xlUp).Row).Formula = "=SUMIFS($D:$D,$A:$A,""=""&$G2,$B:$B,$H2,$C:$C,""=""&$N$1)"
    If posting code please use code tags, see here.

  6. #6
    Registered User
    Join Date
    11-11-2015
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    3

    Re: Convert Sumifs to vba code with loop

    Guys thank you for your time,

    Both of them work in their way but not exactly what I need, my problem is that I can't really understand or make changes to the code(sorry for my poor vba skills, I just started)

    As for the criteria1 and 2 , this have been extracted from code1+code2 columns aiming to get as a result all the unique pairs. I have already done it so no need for changes there (always a better/efficient way to do it would be good to know)

    My target is to read the already made criteria from G and H columns and do the sums. After filling the cell go to next cell of column K and do that again (based on the new Criteria).


    IF(AND(HaveTime=1;HaveGoodMood=1);"please add some description on the code referring to what each part does";"I will try harder and find my way out, Thank you so much for your time")
    Last edited by lakamas; 11-12-2015 at 09:06 AM.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Convert Sumifs to vba code with loop

    Hi lakamas,

    My last post with the lakamasII routine is driven by the G & H entries

    You're welcome and thanks for the rep

    Sub lakamasII(): Dim M As Long, Criteria As String, r As Long
                'Clear the Output Column
                Columns("K").CurrentRegion.Offset(1, 0).ClearContents
                'Get the Month wanted from N1 and initiate a dictionary
             M = Cells(1, "N"): With CreateObject("Scripting.Dictionary")
                'Well scan all the entries in Column A to Load our dictionary
                    For r = 2 To Range("A" & Rows.count).End(xlUp).Row
            'We'll key our dictionary to the (concatenated with a pipe) first three column entries
                    Criteria = Trim(Range("A" & r)) & "|" & _
                    Trim(Range("B" & r)) & "|" & Val(Range("C" & r))
                'If we've already seen that key we'll add to its sum total
                    If .Exists(Criteria) Then
                    .Item(Criteria) = .Item(Criteria) + Val(Range("D" & r))
                    Else: .Item(Criteria) = Val(Range("D" & r))
                    End If: Next
                                    'Now well scan the Column G entries
                            For r = 2 To Range("G" & Rows.count).End(xlUp).Row
                'Keying on the G & H Columns and the wanted month (concatenated with a pipe)
                            Criteria = Trim(Range("G" & r)) & "|" & _
                                        Trim(Range("H" & r)) & "|" & M
                  ' If that Key has been loaded we'll put the total in Colmn K
                            'or else we'll put a zero in that row
                            If .Exists(Criteria) Then _
                                Range("K" & r) = .Item(Criteria) Else Range("K" & r) = 0
                            Next r: End With: End Sub

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Convert Sumifs to vba code with loop

    Worksheet Change event code.
    1) If Cell N1 changed, call "test" procedure.

    test procedure code
    1) Store data in an array, variable a, to speed up the process.
    2) Loop through the data and find the rows that have N1 value in Co.C
    3) When found, store corresponding data to the Dictionary under unique key of Col.A & Col.B.
    4) When duplicates found, add amount of Col.D to the Dictionary item.
    5) OutPut the result.

  9. #9
    Registered User
    Join Date
    11-11-2015
    Location
    Athens
    MS-Off Ver
    2013
    Posts
    3

    Re: Convert Sumifs to vba code with loop

    @xladept the code works great and I understood everything, thank you.
    I just need one more thing now. When all data are in the same worksheet everything works fine but in my case I have the collums a,b,c and d in sheet1 and the rest in sheet2.
    I tried to replace "A" with Sheet1!A but it didnt work , any ideas?

    @jindon thank you for your time and efford.

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Convert Sumifs to vba code with loop

    I think I got them All

    Sub lakamasII(): Dim M As Long, Criteria As String, r As Long
                        Dim w1 As Worksheet, w2 As Worksheet
                    Set w1 = Sheets("Sheet1"): Set w2 = Sheets("Sheet2")
                w2.Columns("K").CurrentRegion.Offset(1, 0).ClearContents
             M = w2.Cells(1, "N"): With CreateObject("Scripting.Dictionary")
                    For r = 2 To w1.Range("A" & Rows.count).End(xlUp).Row
                    Criteria = Trim(w1.Range("A" & r)) & "|" & _
                    Trim(w1.Range("B" & r)) & "|" & Val(w1.Range("C" & r))
                    If .Exists(Criteria) Then
                    .Item(Criteria) = .Item(Criteria) + Val(w1.Range("D" & r))
                    Else: .Item(Criteria) = Val(w1.Range("D" & r))
                    End If: Next r
                            For r = 2 To w2.Range("G" & Rows.count).End(xlUp).Row
                            Criteria = Trim(w2.Range("G" & r)) & "|" & _
                                        Trim(w2.Range("H" & r)) & "|" & M
                            If .Exists(Criteria) Then _
                                w2.Range("K" & r) = .Item(Criteria) Else w2.Range("K" & r) = 0
                            Next r: End With: End Sub

+ 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] Convert SUMIFS formula to SUMPRODUCT
    By timbury1 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 11-08-2014, 01:18 PM
  2. VBA Sumifs Loop
    By SimonJF in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-08-2013, 12:18 AM
  3. Loop function SUMIFS
    By jnh0 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-05-2013, 12:35 PM
  4. convert sumifs to run it on excel 2003
    By albert28 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-01-2013, 06:30 AM
  5. [SOLVED] convert sumifs
    By rana_shaker in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-02-2012, 12:15 PM
  6. [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
  7. Convert SUMIFS formula for 2003
    By SAsplin in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-30-2009, 07:40 AM

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