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.
 

SteveO59L

Well-known Member
Joined
Apr 21, 2004
Messages
7,896
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
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
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
 

drsarao

Well-known Member
Joined
Sep 9, 2009
Messages
1,112
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
 

Hawaiin punch

New Member
Joined
Mar 8, 2010
Messages
3
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
 

Forum statistics

Threads
1,085,370
Messages
5,383,241
Members
401,820
Latest member
RustEE2020

Some videos you may like

This Week's Hot Topics

Top