vba help - to shorten working dictionary code

Mallesh23

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

I am using below macro works perfectly.
Task was to Make sum of cell values .... exclude strings , #N/A, blank cells..

Is there any alternate way to check below line . just for learning purpose.
Add arr(i, 1), Array(IIf(Application.IsNumber(arr(i, 2)), arr(i, 2), 0), IIf(Application.IsNumber(arr(i, 3)), arr(i, 3), 0), Format(arr(i, 4), "Dd/mm/yyyy"))

if in future Dictionary Items may increase, IIF condition will be lengthy .



VBA Code:
Sub test()
Dim arr As Variant
arr = Range("A1").CurrentRegion.Value2

Dim i As Long
Dim dict As New Scripting.Dictionary

With dict

        For i = LBound(arr) + 1 To UBound(arr)
            If Not .Exists(arr(i, 1)) Then
                .Add arr(i, 1), Array(IIf(Application.IsNumber(arr(i, 2)), arr(i, 2), 0), IIf(Application.IsNumber(arr(i, 3)), arr(i, 3), 0), Format(arr(i, 4), "Dd/mm/yyyy"))
            Else
                temp = .Item(arr(i, 1))
                temp(0) = temp(0) + IIf(Application.IsNumber(arr(i, 2)), arr(i, 2), 0)
                temp(1) = temp(1) + IIf(Application.IsNumber(arr(i, 3)), arr(i, 3), 0)
                .Item(arr(i, 1)) = 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(, 3).Value = .Item(c.Value)(2)
            End If
        Next c
    End With
End Sub


Below is a table with output.......... In future Columns may increase , how to put 10 columns into dictionary Items,

Book10
ABCDEFGHI
1PlayerCommissionedSalary ReceivedPayment DatePlayerCommissionedSalary ReceivedPayment Date
2Sachin1000400022/10/2020Sachin30001100022/10/2020
3Dhoni#N/A500023/10/2020Dhoni100002000023/10/2020
4Virat1000500024/10/2020Virat20001400024/10/2020
5Sachin100025/10/2020
6Dhoni5000700026/10/2020
7Dhoni5000800026/10/2020
8Sachinxxx600026/10/2020
9Sachin1000100026/10/2020
10Virat1000900026/10/2020
Sheet1
 
Hi Domenic and Rick

Perfect ! it worked, getting output as expected.(y)

Thanks a lot for sharing wonderful code. ? ??


Thanks
mg
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
@Rick Rothstein

When I run Domenic's code, I get extra information in Columns P, Q and R. Do you not get that as well? Or, perhaps, Mallesh23 is asking for that extra information and I just missed where he did that?

Hi Rick, yes, my original code output all columns. However, in my last post, I amended the code to output only the columns as the OP subsequently specified.

Cheers!
 
Upvote 0
@Mallesh23 , @Rick Rothstein

Oops, my apologies. It looks like only those select columns were actually required after the OP made the initial change. I just didn't notice it. :oops:

Oh well. :)

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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