Array attached to Dictionary Not Updating...

bradmsg

New Member
Joined
Jan 30, 2023
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have a dictionary from sheet 1. Im checking sheet 2 (Col W) with the first 2 parts of the key from the dictionary.

if the key exists, populate an array of counts for each type, per key.

In the end i would just like to output them to a sheet 3.

Problem:

The counts are not populating. in any of the debug statements, they are coming up blank.... Can anyone spot where I'm messing up?




VBA Code:
Dim data2 As Variant
data2 = Sheets("Sheet2").Range("W1:W" & Sheets("Sheet2").Cells(Rows.Count, "W").End(xlUp).Row).value
Dim key As String
Dim value As String
Dim counts() As Long
For i = 2 To UBound(data2, 1)
    key = ""
    value = ""
    counts = Array(0, 0, 0, 0, 0)
    arr = Split(data2(i, 1), "|")
    key = arr(0) & "|" & arr(1)
    value = arr(2)

    If dict.Exists(key) Then
        counts = dict.Item(key)
        Debug.Print "Before update: key=" & key & ", value=" & value & ", counts=" & Join(counts, ", ")
        Select Case value
            Case "1-Emergency"
                counts(0) = counts(0) + 1
            Case "3A - High Sustain"
                counts(1) = counts(1) + 1
            Case "3B - Medium Sustain"
                counts(2) = counts(2) + 1
            Case "4A - High Enhance"
                counts(3) = counts(3) + 1
            Case "Type 5"
                counts(4) = counts(4) + 1
        End Select
        Debug.Print "After update: key=" & key & ", value=" & value & ", counts=" & Join(counts, ", ")
        dict.Item(key) = counts
        Debug.Print "After dict update: key=" & key & ", counts=" & Join(dict.Item(key), ", ")
    End If
Next i
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi @bradmsg, thanks for posting on the forum.

I'm sorry to tell you that you have several problems in your macro.

I list them below:
1. These variables need to be declared.​
VBA Code:
  Dim i As Long
  Dim arr As Variant
  Dim dict As Object
2. Missing set dict object
VBA Code:
  Set dict = CreateObject("Scripting.Dictionary")
3. This condition is never met because you have not added any keys to the dictionary.​
So the macro does not perform any action.​
VBA Code:
If dict.Exists(key) Then

I recommend using the Option Explicit statement at the beginning of your code, it will force you to declare all your variables, but it helps you check the syntax of some instructions, it also checks for writing errors in variables among other things. :cool:

To fix your code, you need to explain to me what you need, what is the end goal.
Put example data and the final result you need.

It is very important that you put the examples before the macro and after the macro.
Use XL2BB tool to put the examples. 😉

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------
 
Upvote 0

DanteAmor,​


thank you for your time replying back to me. It seems i posted prematurely and solved my own issue!

Once again thank you for your response!!
 
Upvote 1

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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