Avatar of Alex Krakov
Alex Krakov
 asked on

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.
VBA

Avatar of undefined
Last Comment
Alan

8/22/2022 - Mon
Alan

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.
Alex Krakov

ASKER
Hi Alan.
I attach file. Yes it must be VBA.
data.xlsm
ASKER CERTIFIED SOLUTION
Alan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Alex Krakov

ASKER
Thanks Alan! it's work perfect.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Alan

You're most welcome - glad to help.

Alan.