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.
 

Some videos you may like

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

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,143
Office Version
  1. 2007
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,230
Messages
5,594,947
Members
413,953
Latest member
Arthur1471

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
Top