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,082,167
Messages
5,363,528
Members
400,747
Latest member
monty_gl

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top