vba help - Dictionary key issue

Mallesh23

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

I am printing dictionary keys getting extra keys which are highlighted in Red.

Below are my code.

VBA Code:
Option Explicit

Sub TEST()

Dim ar As Variant

ar = Range("a1").CurrentRegion.Offset(1).Value

    Dim dict As New Scripting.Dictionary
    Dim i As Long
    Dim skey As String
    Dim cAmount As Double
    Dim k As String
    Dim tmp As Variant
    

With dict
    
    For i = LBound(ar, 1) To UBound(ar, 1)
        skey = ar(i, 1) & "|" & ar(i, 2) & "|" & ar(i, 3)
        cAmount = IIf(Application.IsNumber(ar(i, 6)), ar(i, 6), 0)
        If Not .Exists(skey) Then
            .Item(skey) = Array(ar(i, 4), ar(i, 5), cAmount)
        Else
            tmp = .Item(skey)
            tmp(2) = tmp(2) + cAmount
            .Item(skey) = tmp
        End If
    Next i
    
    Range("I2").Resize(.Count).Value = WorksheetFunction.Transpose(.Keys)
    Range("J2").Resize(.Count).Value = Application.Index(.Items, 0, 1)
    Range("K2").Resize(.Count).Value = Application.Index(.Items, 0, 2)
    Range("L2").Resize(.Count).Value = Application.Index(.Items, 0, 3)


End With

End Sub

Getting wrong value in Range("I6"). and Range("L6")

Book3
ABCDEFGHIJKL
1C1C2C3InvoiceTrn IDNotionalUniqueInvoiceTrn IDNotional
2XYZ100050001223X|Y|Z1000500011648
3PQR20006000#N/AP|Q|R200060005000
4ABC300070004632A|B|C3000700012552
5XYZ2001433395X|Y|N700020002606
6PQR180115||0
7ABC1241611920
8XYZ1631593030
9XYN700020002606
10XYZ100050004000
11PQR200060005000
12ABC300070006000
Sheet1



Thanks
mg
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
ar = Range("a1").CurrentRegion.Offset(1).Value

By offsetting A1.CurrentRegion by one row, you are including a blank row at the bottom (row 13 in your example), hence that extra value you don't want

Try loading ar like this instead
VBA Code:
With Range("A1").CurrentRegion
  ar = .Offset(1).Resize(.Rows.Count - 1).Value
End With
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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