Generating the report using Sumifs

srlakra

New Member
Joined
Mar 24, 2015
Messages
23
Hi,

I just wanted to generate the report using the macro but something went wrong. Can any one assist please.
Sheet 1 contains the data. headers are the name of the ranges.

RegionTypePro_Type Amt
NorthRetail020237.00
EastProjectH9552.00
WestRetail024832.00
SouthProjectR16111.00
NorthRetail09192.00
NorthRetail05765.00
EastRetail019960.00
EastRetail021645.00
EastProjectR19418.00
EastProjectR22599.00
WestProjectC18368.00
WestProjectC14860.00
WestProjectH11779.00
WestProjectR6304.00
WestProjectR9508.00
NorthRetail03324.00
NorthRetail013292.00
NorthRetail017061.00

<colgroup><col width="76" span="4" style="width:57pt"> </colgroup><tbody>
</tbody>

<tbody>
</tbody>


Sheet 2

abc
1
2
3Select typeThis Contains Validation (Retail,Project,H,R,C)
4
5
6
7
8
Region

<tbody>
</tbody>
Sale

<tbody>
</tbody>
9
North

<tbody>
</tbody>
10
East

<tbody>
</tbody>
11
West

<tbody>
</tbody>
12
South

<tbody>
</tbody>

<tbody>
</tbody>

Code that I've used:

Sub rep()


Dim Amt As Range
Dim Region As Range
Dim Pro_Type As Range
Dim I As Integer


For I = 9 To 12


If Cells(3, "B") = "Retail" Then

Cells(I, "C") = worksheetsfunction.SumIfs(Amt, Region, Cells(I, "B"), Pro_Type, "0")

Else

If Cells(3, "B") = "Project" Then

Cells(I, "C") = worksheetsfunction.SumIfs(Amt, Region, Cells(I, "B"), Pro_Type, "<>" & "0")


Else

Cells(I, "C") = worksheetsfunction.SumIfs(Amt, Region, Cells(I, "B"), Pro_Type, Cells(3, "B"))


End If


Next I


End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I have further corrected the code
Sub rep()


Dim Amt As Range
Dim Region As Range
Dim Pro_Type As Range
Dim i As Long


Set Amt = Range("Amt")
Set Region = Range("Region")
Set Pro_Type = Range("Pro_Type")


For i = 9 To 12


If Cells(3, "B") = "Retail" Then

Cells(i, "C") = WorksheetFunction.SumIfs(Amt, Region, Cells(i, "B"), Pro_Type, "0")

Else

If Cells(3, "B") = "Project" Then

Cells(i, "C") = WorksheetFunction.SumIfs(Amt, Region, Cells(i, "B"), Pro_Type, "<>" & "0")


Else

Cells(i, "C") = WorksheetFunction.SumIfs(Amt, Region, Cells(i, "B"), Pro_Type, Cells(3, "B"))


End If


Next i


End Sub
 
Upvote 0
I just wanted to generate the report using the macro but something went wrong

something is a bit vague; tell us more. what did you want to do (in english, not in code)? what was the result? what is wrong in the results?
 
Upvote 0
I changed the layout of the code using indentation. Every time you start a (if..else..end if / for..next) block, indent the code. At the end of the block unindent. It makes your code so much easier to read.
By doing that I noticed that an 'end if' was missing.
Check the code below.
Code:
Option Explicit

Sub rep()

Dim Amt As Range
Dim Region As Range
Dim Pro_Type As Range
Dim i As Long

Set Amt = Range("Amt")
Set Region = Range("Region")
Set Pro_Type = Range("Pro_Type")

For i = 9 To 12
   If Cells(3, "B") = "Retail" Then
      Cells(i, "C") = WorksheetFunction.SumIfs(Amt, Region, Cells(i, "B"), Pro_Type, "0")
   Else
      If Cells(3, "B") = "Project" Then
         Cells(i, "C") = WorksheetFunction.SumIfs(Amt, Region, Cells(i, "B"), Pro_Type, "<>" & "0")
      Else
         Cells(i, "C") = WorksheetFunction.SumIfs(Amt, Region, Cells(i, "B"), Pro_Type, Cells(3, "B"))
      End If
   End If
Next i

End Sub
 
Upvote 0
I want that once i run the macro the report should generate in sheet 2 basis the validation. Like Retail, Project, H, R, C for different regions. Sum of amount.
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

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