excel - Removing repeated array items - VBA -
I have a question in Excel VBA:
Let's say I have an array: {A, b, c, d, a, a, b, e, f, d, c, e, g, f, e}, where some elements are repeated.
I would like to get a list of all the elements without repetition: i.e. {a, b, c, d, e, f, g}
I have received a code that duplicates Removes elements (), but then I get {g} because it is not just repetitive.
I appreciate any suggestion or help!
Thanks a lot!
I have already seen it for you. In the first example, the collection will contain only unique items. You can loop on the collection to gain access to them. In the second example I use a dictionary - and you can get unique items a little more easily.
Here you are:
sub arr_parse_test () New RAVA = Array ("a" as integer dim dick in the form of varialt DIM as Dim RRA , "B", "B", "D", "G", "A", "B", "Z"), start the error again. I = 0 for the UNDound (RRA). Ral (I) CSRR (RRI (i)) If (Err.Number & lt; & gt; 0) then Err.Clear Next I End sub here for completion scripting.dictionary is an example. . It is often preferred over the VBA archive. As a bonus, it is easy to recover unique items. Your unique item is pressed into version / array rra2 . See it
sub arr_parse_test2 () Egg Array, RRI 2 as variable dim as integer DICT Object set dict = object set ("scripting dark") RRR = array ("A", "B", "C", "D", "G", "A", "B", "Z") If i = 0 for Ubound (RRA) for (if not decat. Axisists (CSRR (ar) (i)) then dac Add CSRR (RRI (I)), RRA (i) The next IRR2 = decat.it sub This works because this VBA archive and MS (Scripting) Dictionary only Accept items with the unique key will forget the error while trying to add an item with the already existing key in the archive collection. Therefore, start the next statement on the error
with a dictionary, you can test that a key already exists (in dictionary Dict.Exists ), before adding. You can learn more about compilations and dictionaries.
Comments
Post a Comment