Assigning dictionary data to an array...

DashBoarder

New Member
Joined
Sep 9, 2014
Messages
9
Greetings to all,

When using an Excel dictionary, and when using either the .Items or the .Keys methods to assign the dictionary's items or keys to an array, will the array always be cleared of any existing data before the assignment is made?

I'm working on a program which loops through data records, and for each record a dictionary is used to collect a unique list of phone numbers (as keys), which then gets assigned to an array. For each loop iteration, the same array is used to collect the dictionary data. I want to make sure that each time the dictionary data is assigned to the array that there are no old entries lingering within the array.

My experiments seems to indicate that indeed the array is cleared of any old data when the assignment is made - but I would like to hear from others on this matter for a more conclusive opinion, or perhaps someone could steer me to some documentation that states this unequivocally.

THANKS!!!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
If you're re-assigning the information to where it was in memory in the array with the phone number than it will be cleared. If you're adding to the array then maybe it won't, just depends how you've programmed it.
 
Last edited by a moderator:
Upvote 0
If you are using something like
Code:
MyArray=Dic.keys
then you will be ok.
Otherwise it depends. If you post your code, we can have a look at it.
 
Upvote 0
If you are using something like
Code:
MyArray=Dic.keys
then you will be ok.
Otherwise it depends. If you post your code, we can have a look at it.

RileyC & Fluff,

Thank you for your prompt replies!

I'm not quite sure just what you mean here, but I can show you some code. Here's the function that I use; it takes an array of (possibly) non-unique phone numbers, removes any repeated numbers, and then returns the unique list of numbers to yet another array...

Code:
Function RemoveDupes(InputArray As Variant) As Variant
  'From... [URL]https://www.mrexcel.com/forum/excel-questions/902466-remove-duplicates-1d-array-vba.html[/URL]
  Dim X As Long
  With CreateObject("Scripting.Dictionary")
    For X = LBound(InputArray) To UBound(InputArray)
      If Not IsMissing(InputArray(X)) Then .Item(InputArray(X)) = 1
    Next
    RemoveDupes = .Keys
  End With
End Function
If varNONUniquePhoneNos() is my input array of phone numbers with (possibly) repeated entries, and if varUNIQUEPhoneNos() is the listing of unique phone numbers returned by the functon above, then I would call the function as follows...

Code:
varUNIQUEPhoneNos = RemoveDupes(varNONUniquePhoneNos)
Further, here's a little test program...

Code:
Sub DictionaryTest3()
    Dim varNONUniquePhoneNos(8) As Variant, varUNIQUEPhoneNos() As Variant
    Dim intArrayIndex As Integer

    varNONUniquePhoneNos(1) = "0987654321"
    varNONUniquePhoneNos(2) = "1357924680"
    varNONUniquePhoneNos(3) = "0987654321"
    varNONUniquePhoneNos(4) = "2224446666"
    varNONUniquePhoneNos(5) = "3335557777"
    varNONUniquePhoneNos(6) = "3335557777"
    varNONUniquePhoneNos(7) = "5557779999"
    
    varUNIQUEPhoneNos = RemoveDupes(varNONUniquePhoneNos)
    
    For intArrayIndex = 1 To UBound(varUNIQUEPhoneNos)
        Debug.Print "varUNIQUEPhoneNos(" & intArrayIndex & ") = "; varUNIQUEPhoneNos(intArrayIndex)
    Next intArrayIndex
End Sub
NOTE that I apologize if I haven't formatted my code appropriately; I'm not so familiar with this site.
 
Upvote 0
As the array varUNIQUEPhoneNos doesn't have any entries before you call the Function, there wont be a problem.
 
Upvote 0
As the array varUNIQUEPhoneNos doesn't have any entries before you call the Function, there wont be a problem.

Fluff,

Yes, I understand that. But I'm asking about more general situations - like the one I described in my original message. If the varUNIQUEPhoneNos() array is used over and over, each time a record's phone numbers are collected, then are its contents cleared each time it's assigned the results of the RemoveDupes() function.

Thanks!
 
Upvote 0
Yes, because you are filling the array in much the same manner I showed in post#3
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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