SUMIFS ranges in a nested loop question

Reziukas

New Member
Joined
Dec 1, 2014
Messages
1
Dear All,

Pretty much a total noob here :)

I'm trying to build a sumifs function with two conditions (transaction date and transaction code which are in columns of unique values) into a nested loop. tr_date and tr_code return string values which are a date and an alphanumeric code which seem to return correct values in watch window, but the sumifs returns 0 values for all but 3 codes (which repeat more than 3 times) from a list of 300+ codes of 7 types so I'm guessing there may be something wrong with how I did sumifs ranges. Any help is much appreciated. Code below

Code:
Sub trying3()


Worksheets("Report").Activate


Application.DisplayAlerts = False


Dim last_date As Long
last_date = Range("AC1").End(xlDown).Row


Dim date_count As Long
date_count = 2


Dim tr_date As String
tr_date = Range("AC" & date_count)


Dim last_code As Long
last_code = Range("AD1").End(xlDown).Row


Dim code_count As Long
code_count = 2


Dim tr_code As String




Dim tgt_rng_cnt As Long
tgt_rng_cnt = 2


Dim i As Long
i = 2


Dim ppdate As Range
Dim ppval As Range
Dim code As Range


Set ppdate = Worksheets("Report").Range("D2:D20000")
Set ppval = Worksheets("Report").Range("G2:G20000")
Set code = Worksheets("Report").Range("J2:J20000")


    For date_count = 2 To last_date
        
        For code_count = 2 To last_code
                            
                           tr_date = Range("AC" & date_count)
                           
                           tr_code = Range("AD" & code_count)
                            
                            Cells(i, 31) = tr_code
                            Cells(i, 32) = tr_date
                            Cells(i, 32) = Format(Cells(i, 32), "dd/mm/yyyy")
                            Cells(i, 33) = Application.WorksheetFunction.SumIfs(ppval, ppdate, tr_date, code, tr_code)
                     
                     i = i + 1
                               
            Next code_count
      
        Next date_count

End Sub

P.S. I'm pretty sure there's a "trash" variable there, I just can't be bothered to take it out tonight :)
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try changing
Dim tr_date as String
to
Dim tr_date as Long

Also, you can combine
Code:
                            Cells(i, 32) = tr_date
                            Cells(i, 32) = Format(Cells(i, 32), "dd/mm/yyyy")
to
Code:
                            Cells(i, 32) = Format(tr_date, "dd/mm/yyyy")
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,699
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top