VBA SUMIF with conditions

briguyUSA

New Member
Joined
Jun 10, 2021
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I need assitance with a SUMIF in vba based on a condition.

This is an example of what the data is like. I have multiple of account numbers that is split up into types (apple, pear) with values.
Account NumberTypeValue
123Apple12
456Apple13
234Apple45
123Apple6
123Apple22
456Apple45
123Apple78
123Pear4
123Pear45
456Pear7
234Pear9
987Pear3



In the end, I want to get something like this. I hope to split the result into two that is separated by the Type. The problem i have is that there are similar account numbers that is both an apple type and pear type.

Account Number (Apple)Total ValueAccount Number (Pear)Total Value
12310912349
456584567
234452349
9873
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Roughly how many rows of data do you have?
 
Upvote 0
Ok how about
VBA Code:
Sub briguyUSA()
   Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Master")
      Ary = .Range("A2:C" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
   End With
   
   For i = 1 To UBound(Ary)
      If Not Dic.Exists(Ary(i, 2)) Then Dic.Add Ary(i, 2), CreateObject("scripting.dictionary")
      Dic(Ary(i, 2))(Ary(i, 1)) = Dic(Ary(i, 2))(Ary(i, 1)) + Ary(i, 3)
   Next i
   With Sheets("Sheet2")
      For i = 0 To Dic.Count - 1
         .Cells(1, i * 3 + 1).Value = Dic.Keys()(i)
         .Cells(2, i * 3 + 1).Resize(Dic.Items()(i).Count, 2).Value = Application.Transpose(Array(Dic.Items()(i).Keys, Dic.Items()(i).Items))
      Next i
   End With
End Sub
 
Upvote 0
Solution
Ok how about
VBA Code:
Sub briguyUSA()
   Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
  
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Master")
      Ary = .Range("A2:C" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
   End With
  
   For i = 1 To UBound(Ary)
      If Not Dic.Exists(Ary(i, 2)) Then Dic.Add Ary(i, 2), CreateObject("scripting.dictionary")
      Dic(Ary(i, 2))(Ary(i, 1)) = Dic(Ary(i, 2))(Ary(i, 1)) + Ary(i, 3)
   Next i
   With Sheets("Sheet2")
      For i = 0 To Dic.Count - 1
         .Cells(1, i * 3 + 1).Value = Dic.Keys()(i)
         .Cells(2, i * 3 + 1).Resize(Dic.Items()(i).Count, 2).Value = Application.Transpose(Array(Dic.Items()(i).Keys, Dic.Items()(i).Items))
      Next i
   End With
End Sub
I thought a SUMIF function would be enough. may i ask why you decided to use a dictionary?
 
Upvote 0
Because it's quicker & simpler than using worksheet functions in VBA.
 
Upvote 0
worked like a charm!! do you have any recommendations on VBA sites /materials to study from?
 
Upvote 0
You're welcome & thanks for the feedback.

Not really, I've learnt most of what I know from this site.
 
Upvote 0

Forum statistics

Threads
1,215,019
Messages
6,122,707
Members
449,093
Latest member
Mnur

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