SUMIFS in VBA - return values on multiple columns dependent on a cell header

ramona_manea

New Member
Joined
Oct 20, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello all. I have a raw data of sales (This year, last year, budget ) by stores (380 stores), by each day of the 2022 year.
I am trying to build a macro that brings me the data from that RAW database to another sheet , where the data is presented a little differently, analyzed etc .
First I tried to do it with simple SUMIFS formula in Excel and the file almost crashed. Then I created a VBA with SUMIFS , but also it takes a lot of time to update. And I could get past that problem, but I do not know how to make it populate data in all columns based on a header .

I need to present the data with same main categories, but by days . In order for this to happen I am just copy - pasting in VBA the script I wrote and changing the columns in which I want the VBA to put the data and also the cell of date that is changing. I am writing below my script. Could you please help? I am sure there is an easier way .

Thanks !

Sub Sumifs()

Dim CA_istoric As Range
Dim CA_real As Range
Dim CA_buget As Range
Dim Clienti_istoric As Range
Dim Clienti_real As Range
Dim Cantitate_istoric As Range
Dim Cantitate_real As Range
Dim Magazine As Range
Dim Data As Range
Dim LastRow As Long
Dim Counter As Integer

Set CA_istoric = Sheets("RAW").Range("T:T")
Set CA_real = Sheets("RAW").Range("J:J")
Set CA_buget = Sheets("RAW").Range("Q:Q")
Set Clienti_istoric = Sheets("RAW").Range("W:W")
Set Clienti_real = Sheets("RAW").Range("M:M")
Set Cantitate_istoric = Sheets("RAW").Range("X:X")
Set Cantitate_real = Sheets("RAW").Range("N:N")
Set Magazine = Sheets("RAW").Range("F:F")
Set Data = Sheets("RAW").Range("B:B")
LastRow = ActiveSheet.Range("I6").End(xlDown).Row

For Counter = 6 To LastRow

ActiveSheet.Range("M" & Counter) = WorksheetFunction.Sumifs(CA_real, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("M4") + 1)
ActiveSheet.Range("S" & Counter) = WorksheetFunction.Sumifs(Clienti_real, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("M4") + 1)
ActiveSheet.Range("U" & Counter) = WorksheetFunction.Sumifs(Cantitate_real, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("M4") + 1)
ActiveSheet.Range("N" & Counter) = WorksheetFunction.Sumifs(CA_buget, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("M4") + 1)
ActiveSheet.Range("Q" & Counter) = WorksheetFunction.Sumifs(CA_istoric, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("M4") + 1)
ActiveSheet.Range("AC" & Counter) = WorksheetFunction.Sumifs(Clienti_istoric, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("M4") + 1)
ActiveSheet.Range("V" & Counter) = WorksheetFunction.Sumifs(Cantitate_istoric, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("M4") + 1)

ActiveSheet.Range("AG" & Counter) = WorksheetFunction.Sumifs(CA_real, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("AG4") + 1)
ActiveSheet.Range("AH" & Counter) = WorksheetFunction.Sumifs(CA_buget, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("AG4") + 1)
ActiveSheet.Range("AK" & Counter) = WorksheetFunction.Sumifs(CA_istoric, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("AG4") + 1)
ActiveSheet.Range("AM" & Counter) = WorksheetFunction.Sumifs(Clienti_real, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("AG4") + 1)
ActiveSheet.Range("AO" & Counter) = WorksheetFunction.Sumifs(Cantitate_real, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("AG4") + 1)
ActiveSheet.Range("AP" & Counter) = WorksheetFunction.Sumifs(Cantitate_istoric, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("AG4") + 1)
ActiveSheet.Range("AW" & Counter) = WorksheetFunction.Sumifs(Clienti_istoric, Magazine, ActiveSheet.Range("H" & Counter).Value, Data, ActiveSheet.Range("AG4") + 1)

Next Counter


End Sub

Book1
HIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZ
401/10/202202/10/2022
5Store codeStore nameActualBugetN-1Clienti#Nr articole#Nr articole N-1Clienti N-1ActualBugetN-1Clienti#Nr articole#Nr articole N-1Clienti N-1
61A
72B
83C
94D
105E
Oct
Cells with Conditional Formatting
CellConditionCell FormatStop If True
AG4Cell Value="duminică"textNO
AG4Cell Value="duminica"textNO
M4Cell Value="duminică"textNO
M4Cell Value="duminica"textNO


Book1
ABCDEFGHIJKLMNOPQRSTUVWXY
1Data N-1DataZi SaptamanaCod MagazinMagazinCod SAPDenumire SAPFormatRegiuneCA TTC TotalNr Clienti Total Cantitate Total Buget TTC TotalCA TTC N-1 TotalNr Clienti N-1 TotalCantitate N-1 Total
201/10/2022Saturday1A102,872.802,08214,64986,344.72108,051.492,01516,589
301/10/2022Saturday2B0.000062,176.870.0000
402/10/2022Sunday1A17,310.970031,775.0714,024.8600
502/10/2022Sunday2B24,588.44651560.0021,331.1242132
602/10/2022Sunday3C18,141.7142930.0011,907.7544119
703/10/2022Sunday1A12,102.08581580.008,832.653083
803/10/2022Sunday2B11,570.20581430.008,580.393188
RAW
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Im pretty sure it will be easier to use a pivot table on your raw data.
 
Upvote 0
I have extra columns and other dependent calculations in the sheet I want to bring data to…. The raw data is extracted from a system AS IS
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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