Look up and return a name in the relevant row Loop

Hawaiin punch

New Member
Joined
Mar 8, 2010
Messages
3
Hi

I have a Column A of mixed up names (the mix sometimes contains long codes), eg.

1234_treadmill*
b812 bicycle
ops+keyboard
rf_treadmill-
b812 bicycle_87
ops bicycle_&*
mouse et.<33>

And I have a Column B of simple names, eg.
treadmill
bicycle
keyboard
mouse

I want a macro to go through Column A and search for every value that can be found in Column B, and return the simple name in Column C for every relevant row in Column A. eg.

C
treadmill
bicycle
keyboard
treadmill
bicycle
bicycle
mouse

So far, I have a macro but it gets stuck on the 2nd loop and it also does not return the simple name on relevant row in column A... HELP!

Sub ReturnName()
Dim rngeFound As Range, rngeSearchRange As Range
Dim ref As Variant
Dim i As Variant
Set rngeSearchRange = ActiveSheet.Range("b4:b700")
Set ref = Range("b4")
Set rngeFound = rngeSearchRange.Find(ref)
Do
For Each i In Range("C4:C700")
i.Value = ref
'i = ActiveCell.Offset(1, 0).Select
'i = ref
ref = ref.Offset(1, 0)
Next i
Loop

Your help is much appreciated....

Thanks.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Something along the lines of

Dim c1 As Range, rng1
Dim c2 As Range, rng2
Dim val1 As String

Set rng1 = Range("a1:a7")
Set rng2 = Range("b1:b4")

For Each c2 In rng2
val1 = c2.Value
For Each c1 In rng1
If InStr(c1, val1) Then
c1.Offset(0, 2) = val1
End If
Next c1
Next c2
 
Upvote 0
Try:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG08Mar15
[COLOR="Navy"]Dim[/COLOR] RngA [COLOR="Navy"]As[/COLOR] Range, DnA [COLOR="Navy"]As[/COLOR] Range, RngB [COLOR="Navy"]As[/COLOR] Range, DnB [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] RngA = Range(Range("A1"), Range("A" & Rows.count).End(xlUp))
[COLOR="Navy"]Set[/COLOR] RngB = Range(Range("B1"), Range("B" & Rows.count).End(xlUp))
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] DnB [COLOR="Navy"]In[/COLOR] RngB
        [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] DnA [COLOR="Navy"]In[/COLOR] RngA
            [COLOR="Navy"]If[/COLOR] InStr(DnA, DnB) [COLOR="Navy"]Then[/COLOR]
                DnA(1, 3) = DnB
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]Next[/COLOR] DnA
    [COLOR="Navy"]Next[/COLOR] DnB
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Welcome to MrExcel.
Another possible solution:
Code:
Sub ReturnName()
Dim i, j, n, LastRowA, LastRowB
LastRowA = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
LastRowB = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To LastRowA
    For j = 4 To LastRowB
        n = InStr(1, Cells(i, "A"), Cells(j, "B"))
        If n > 0 Then Cells(i, "C") = Cells(j, "B")
    Next j
Next i
End Sub
 
Upvote 0
Hi!

That was quick thank you so much guys - this is working perfectly fine on one sheet... but my column A and column C are actually in Sheet1 and column B (the list of simple names) is in Sheet2.

I tried changing it to worksheets("Sheet2").range.... but the macro just seems to keep on running and returns nothing!

Again, thanks for your help...

Cheers
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,696
Members
448,293
Latest member
jin kazuya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top