vba help, sum with dictionary exclude blank/#N/A/string value

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

While summing data in dictionary, sometimes I come across with string/Blank/#N/A value in column.
how to exclude such type of cells values.

Sub SumUsing_Dictionary_ExcludeBlank_String
VBA Code:
()

    Dim arr As Variant
    arr = Range("A1").CurrentRegion.Value2
  
    Dim temp As Variant
  
    Dim dict As New Scripting.Dictionary

Dim i As Long

    With dict
        For i = LBound(arr) To UBound(arr)
                  
            If Not .Exists(arr(i, 2)) Then
                .Add arr(i, 2), Array(arr(i, 3), arr(i, 4))
            Else
                temp = .Item(arr(i, 2))
                temp(0) = temp(0) + .Item(arr(i, 3))
                temp(1) = temp(1) + .Item(arr(i, 4))
                 .Item(arr(i, 2)) = Temp             
            End If
      
        Next i

  
    Dim c As Range
  
    For Each c In Range("K2:K4")
        If .Exists(c.Value) Then
            c.Offset(, 1).Value = .Item(c.Value)(0)
            c.Offset(, 2).Value = .Item(c.Value)(1)
        End If
  
    Next c
      
    End With
  
  
 
End Sub



Below is a table with expected values are in H and I Columns.


Book17
ABCDEFGHI
1DatePlayerCommissionedSalary ReceivedPlayerCommissionedSalary Received
222/10/2020Sachin10004000Sachin300015000
323/10/2020Dhoni10005000Dhoni200020000
424/10/2020Virat1000xxxxVirat20009000
525/10/2020Sachin10004000
626/10/2020Dhoni10007000
726/10/2020Dhoni#N/A8000
826/10/2020Sachin10006000
926/10/2020Sachin1000
1026/10/2020Virat10009000
Sheet1



Thanks
mg
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi
Try to replace with
VBA Code:
With dict
        For i = LBound(arr) + 1 To UBound(arr)
                  
            If Not .Exists(arr(i, 2)) Then
            
                .Add arr(i, 2), Array(IIf(Application.IsNumber(arr(i, 3)), arr(i, 3), 0), IIf(Application.IsNumber(arr(i, 4)), arr(i, 4), 0))
            Else
            
                temp = .Item(arr(i, 2))
                temp(0) = temp(0) + IIf(Application.IsNumber(arr(i, 3)), arr(i, 3), 0)
                temp(1) = temp(1) + IIf(Application.IsNumber(arr(i, 4)), arr(i, 4), 0)
                 .Item(arr(i, 2)) = temp
            End If
      
        Next i
 
Upvote 0
Hi Mohadin,

Superrbbb ! very nice , it worked. thanks a lot for your help ! ?



Thanks
mg
 
Upvote 0
You Are Welcome
Thank you for the feedback
Be Happy
 
Upvote 0
Hi Mohadin,

If I would like to add date column in dictionary items.
What will be the code, as shown in Column J. Any date is ok, first occcurance date or last occurance date from column J.

Below is a table with expected output in Column G onward.

Book17
ABCDEFGHIJ
1DatePlayerCommissionedSalary ReceivedPayment DatePlayerCommissionedSalary ReceivedPayment Date
222/10/2020Sachin1000400022/10/2020Sachin30001500022/10/2020
323/10/2020Dhoni#N/A500023/10/2020Dhoni20002000023/10/2020
424/10/2020Virat1000500024/10/2020Virat2000900024/10/2020
525/10/2020Sachin100025/10/2020
626/10/2020Dhoni1000700026/10/2020
726/10/2020Dhoni1000800026/10/2020
826/10/2020Sachinxxx600026/10/2020
926/10/2020Sachin1000100026/10/2020
1026/10/2020Virat1000900026/10/2020
Sheet1



Thanks
mg
 
Upvote 0
VBA Code:
 With dict
        For i = LBound(arr) + 1 To UBound(arr)
            If Not .Exists(arr(i, 2)) Then
                .Add arr(i, 2), Array(IIf(Application.IsNumber(arr(i, 3)), arr(i, 3), 0), IIf(Application.IsNumber(arr(i, 4)), arr(i, 4), 0), arr(i, 1))
            Else
                temp = .Item(arr(i, 2))
                temp(0) = temp(0) + IIf(Application.IsNumber(arr(i, 3)), arr(i, 3), 0)
                temp(1) = temp(1) + IIf(Application.IsNumber(arr(i, 4)), arr(i, 4), 0)
                .Item(arr(i, 2)) = temp
            End If
        Next i
        Dim c As Range
        For Each c In Range("K2:K4")
            If .Exists(c.Value) Then
                c.Offset(, 1).Value = .Item(c.Value)(0)
                c.Offset(, 2).Value = .Item(c.Value)(1)
                c.Offset(, 2).Value = .Item(c.Value)(2)
            End If
        Next c
    End With
 
Upvote 0
Hi Mohadin,

Thanks again for your help.
What this line means :=> .Item(arr(i, 2)) = temp

from below code
VBA Code:
Else
                temp = .Item(arr(i, 2))
                temp(0) = temp(0) + IIf(Application.IsNumber(arr(i, 3)), arr(i, 3), 0)
                temp(1) = temp(1) + IIf(Application.IsNumber(arr(i, 4)), arr(i, 4), 0)
                .Item(arr(i, 2)) = temp

Thanks
mg
 
Upvote 0
You started with
temp = .Item(arr(i, 2))
Then you did some calculations to the content of temp witch is originally the item of arr(i,2)
when you finish you would put the result back to where it belongs, Right?
 
Upvote 0
Hi Mohadin,

oh yes, understood, perfect ! thanks ? (y)


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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