Does VBA have Dictionary Structure Building off cjrhs answer, we can build a Contains function requiring no labels I dont like using labels.Public Function ContainsCol As Collection, Key As String As Boolean.Contains True. On Error Resume Next.Clear. Col Key. If err.Number lt 0 Then.Contains False. Clear.On Error Go. To 0.For a project of mine, I wrote a set of helper functions to make a Collection behave more like a Dictionary.It still allows recursive collections.Youll notice Key always comes first because it was mandatory and made more sense in my implementation.R.png' alt='Set Reference Microsoft Scripting Runtime Vba Array' title='Set Reference Microsoft Scripting Runtime Vba Array' />The RVBA Coding Conventions tell you how to write your code in Visual Basic to be more readable and maintainable.The first thing to do is to create a reference to the Microsoft Scripting Runtime, which is the DLL that contains the Dictionary as well as the FileSystemObject.I also used only String keys.You can change it back if you like.Set. I renamed this to set because it will overwrite old values.Private Sub c. SetBy.Ref Col As Collection, Key As String, Item As Variant.If c. HasCol, Key Then Col.Remove Key. Col. Add ArrayKey, Item, Key.Get. The err stuff is for objects since you would pass objects using set and variables without.I think you can just check if its an object, but I was pressed for time.Private Function c.GetBy. Ref Col As Collection, Key As String As Variant.If Not c. HasCol, Key Then Exit Function.On Error Resume Next.Clear. Set c. Get ColKey1.If err. Number 1.Then. err. Clear.Get ColKey1. On Error Go. Puzzle Kingdoms L Repack Eng 2009 Honda there. To 0. If err. Number lt 0 Then Call err.Number, err. Source, err.Description, err.Help. File, err. Help.Context. Has. The reason for this post.Public Function c.HasCol As Collection, Key As String As Boolean.Has True. On Error Resume Next.Clear. Col Key. If err.Number lt 0 Then.Has False. err. Clear.On Error Go. To 0.Remove. Doesnt throw if it doesnt exist.Just makes sure its removed.Private Sub c. RemoveBy.Ref Col As Collection, Key As String.If c. HasCol, Key Then Col.Remove Key. Keys.Get an array of keys.Private Function c.KeysBy. Ref Col As Collection As String.Dim Initialized As Boolean.Dim Keys As String.For Each Item In Col.If Not Initialized Then.Re. Dim Preserve Keys0.KeysUBoundKeys Item0.Initialized True.Re. Dim Preserve KeysUBoundKeys 1.KeysUBoundKeys Item0.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |