PROBLEM VBA DICTIONARY

roykana

Active Member
Joined
Mar 8, 2018
Messages
311
Office Version
  1. 2010
Platform
  1. Windows
Dear all master,

please help for the problem in the VBA dictionary does not appear because the source is text and the result is a number which I marked in yellow and the other results appear because the source is text
VBA Code:
Sub Demo0301()
Dim Rng As Range, Ds As Range, n As Long, Dic As Object, Source As Variant
Dim Ary As Variant
Application.ScreenUpdating = False
With Sheets("IFG")
    Source = .Range("B1").CurrentRegion.Offset(, 1).Resize(, 2)
End With
Set Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbBinaryCompare
For n = 2 To UBound(Source, 1)
    Dic(Source(n, 1)) = n
Next
With Sheets("MASTER")
    Ary = .Range("c2", .Range("c" & Rows.Count).End(xlUp)).Value2
    ReDim Nary(1 To UBound(Ary), 1 To 3)
    For n = 1 To UBound(Ary)
        If Dic.Exists(Ary(n, 1)) Then
            Nary(n, 1) = Source(Dic(Ary(n, 1)), 2)
'            Nary(n, 2) = Source(Dic(Ary(n, 1)), 3)
'            Nary(n, 3) = Source(Dic(Ary(n, 1)), 4)
        End If
    Next n
    Sheets("MASTER").Range("D2").Resize(UBound(Nary), 1).Value = Nary
End With
 

Attachments

  • RESULT (SHEET MASTER).JPG
    RESULT (SHEET MASTER).JPG
    53.8 KB · Views: 31
  • SOURCE(SHEET IFG).JPG
    SOURCE(SHEET IFG).JPG
    66.4 KB · Views: 32

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Option 1:
You should have data type in col C sheet Master as TEXT. And the easiest way to to convert it to text is using Text to Columns wizard.
See this article:
check this section:
"Convert numbers to text in Excel with Text to Columns wizard"

OR

Option 2:
change this line:
VBA Code:
 If Dic.Exists(Ary(n, 1)) Then
to this:
VBA Code:
 If Dic.Exists(CStr(Ary(n, 1))) Then
 
Upvote 0
Solution
Option 1:
You should have data type in col C sheet Master as TEXT. And the easiest way to to convert it to text is using Text to Columns wizard.
See this article:
check this section:
"Convert numbers to text in Excel with Text to Columns wizard"

OR

Option 2:
change this line:
VBA Code:
 If Dic.Exists(Ary(n, 1)) Then
to this:
VBA Code:
 If Dic.Exists(CStr(Ary(n, 1))) Then
Dear Mr. Akuini,
I use option 2.
Thank you for the code solution from you.
Thanks
roykana
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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