Start the loop again when finding a duplicate in the same cell

I have a Excel file, its look like:
BeforeAfter the macros, this will be the end results:
After
I want macro that read column "B", and paste first country from column "C" in the same cells of column "A", after he find duplicate cell in "B", he copy next country from column "C", and does the same as I wrote with first country and etc.

I have a code:
Sub x()

Dim r1 As Long, r2 As Long

r1 = 2: r2 = 2

Do While Cells(r1, 2) <> vbNullString
    If IsNumeric(Application.Match(Cells(r1, 2), Range(Cells(1, 2), Cells(r1 - 1, 2)), 0)) Then
        r2 = r2 + 1
    End If
    Cells(r1, 1).Value = Cells(r2, 3).Value
    r1 = r1 + 1
Loop

End Sub

Open in new window


But it does not work as it should:
After macro
I want when macro find first duplicate in column "B" (in my case B7 "A Bad Moms Christmas") he starting find duplicate from this cell and not paying attention to past cells, and paste same country from "C" (United Arab Emirates), before he find another duplicate and etc.
Can someone help? Thanks.
Alex KrakovAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlanConsultantCommented:
Hi Alex,

Can you post a sample file rather than us having to re-create it from scratch.

Also, does it have to be VBA?  A formula might be easier, and has the advantage of being self-documenting, and not requiring an XLSM file.

Alan.
0
Alex KrakovAuthor Commented:
Hi Alan.
I attach file. Yes it must be VBA.
data.xlsm
0
AlanConsultantCommented:
Hi Alex,

Try this:

Sub x()

Dim r1 As Long, r2 As Long, r3 As Long

r1 = 2: r2 = 2: r3 = 1

Do While Cells(r1, 2) <> vbNullString
    If IsNumeric(Application.Match(Cells(r1, 2), Range(Cells(r3, 2), Cells(r1 - 1, 2)), 0)) Then
        r2 = r2 + 1
        r3 = r1
    End If
    Cells(r1, 1).Value = Cells(r2, 3).Value
    r1 = r1 + 1
    
Loop

End Sub

Open in new window



Alan.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alex KrakovAuthor Commented:
Thanks Alan! it's work perfect.
0
AlanConsultantCommented:
You're most welcome - glad to help.

Alan.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.