VBA Dictionary ... if word in Row 1 already exits then "word"&"1"

  • Hi there,


    I would be very thankful if some could help me to write a small piece of code which does exactly the following:
    in Row 1: dog cat fish dog mouse duck cat




    Result
    in Row 1: dog cat fish dog1 mouse duck cat1


    I hope it is clear what I am trying to achieve.


    Thanks

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    I forgot to mention that if word appears more than one time, then it should follow this logic, dog dog1 dog2 dog3 and so on... 1, 2, 3, n

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Susy, I've added code tags for you on this occasion but please make sure you do this going forward.


    You could try something like:


  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Ooops, I meant columns.... like the heard of each column... in row 1

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Just change the placement of the row variable in the Cells() argument. Although for the sake of logic you may want to rename that variable:


  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Thanks for your code, but unfortunately it does not work.


    it stops here...

    Code
    .Add (Cells(1, col).Value)


    Thanks

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Thanks anyway. It does not work.

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"



    Using an array list instead.

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Susy..


    Quote

    Thanks anyway. It does not work.


    Saying thanks is always good.. :)


    However.. From the point of view of debugging/testing.. "It does not work".. well.. that will not expedite a solution for you..


    Also.. attaching a Sample Workbook usually increases your chance of help..

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Hi apo,


    you are totally right. Sorry I did not provide a better description of the problem.


    Just imagine a row with many different headers, let say 20 columns with headers... but some headers repeat themselves...


    Dog Cat Fish Cat Horse Chicken Chicken Turkey Dog Cat



    What I am trying to do is just to differentiate those words which repeat themselves with a number at the end of the word like


    Dog Cat Fish Cat1 Horse Chicken Chicken1 Turkey Dog1 Cat2



    Unfortunately I don't know what else to do.

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Try:-

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Hi MickG,


    thanks for this code. It works like a charm.


    Could you explain me how to read this line

    Code
    Dn.Value


    and this one

    Code
    .Add Dn.Value, 0


    and this one too.

    Code
    .Item(Dn.Value) = .Item(Dn.Value) + 1 
                    Dn.Value = Dn.Value & " " & .Item(Dn.Value)


    I am still learning. This is why it will also be very helpful to understand the code.


    Thanks again

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Try this, assumes Headers start in Cell A1


    Edit: As I posted this I saw you already have a solution, this is an alternative method

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Hi KjBox, thanks also for your code. Both codes are fantastic.


    In case you have time, can you also explain me how to understand this:


    Code
    (x, 2)

    , and this


    Code
    x(1, i)

    , and this


    Code
    x(1, i) & ii

    and finally this


    Code
    .toarray


    Thanks in advance.

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    All the headers are load into an array (the variable x) then each element in the array is checked for duplication using the object ArrayList.


    To do the check the code loops through each element in the array

    Code
    x(1, i)


    is the ith element in the 2nd dimension of array x

    Code
    x(1, i) & ii


    concatenates the value of the ith element of the 2nd dimension of array x and the variable ii

    Code
    .toarray


    is a method used with the object ArrayList to transfer the list into an array.


    To understand how arrays work in VBA google "Excel VBA arrays" and have an enjoyable read!

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Code
    Sub test()
        With Cells(1).CurrentRegion.Rows(1)
            .Value = Evaluate(.Address & "&if(countif(offset(" & .Address & ",,,,column(" & .Address & _
            "))," & .Address & ")>1,countif(offset(" & .Address & ",,,,column(" & .Address & _
            "))," & .Address & ")-1,"""")")
        End With
    End Sub
  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Thank for the feedback, Hope this helps !!

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Hi Jindon, thanks for your code.


    as always, it works great. Thanks

  • Re: VBA Dictionary ... if word in Row 1 already exits then "word"&"1"


    Hi MickG,


    amazing. Thanks alot for this great comments on your code, wow... I know comments are not easy to write but they help us, the beginners.


    Thanks for everything

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!