How do I write this loop?

Agent86

Board Regular
Joined
Feb 28, 2003
Messages
229
I've been working on this one for about an hour now, but just can't figure out how to write the correct type of loop.

I have a list of names beginning in cell E20. This list will continue to expand and repeats many of the names. Beginning in cell F20 I have a second list of names that will continue to expand as well. All names in Column F are associated with the name on the same row in Column E.

I'd like to enter a name (from the list in Column E) in cell B5 and generate a new list which would begin in cell C5. This new list should be all the names from Column F whose cooresponding name in Column E matches what was entered in B5.

... I'm sorry if all that sounds confusing. Please let me know if clarification is needed. Thank you in advance fro any guidance you may be able to provide.
 

Some videos you may like

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Derek

Well-known Member
Joined
Feb 16, 2002
Messages
1,592
Hi there

You might like to try this:


Range("E20", Range("E65536").End(xlUp)).Select
For Each cell In Range("E20", Range("E65536").End(xlUp))
If cell.Value = Range("B5").Value Then
Range("C65536").End(xlUp).Offset(1, 0).Value = cell.Offset(0, 1).Value
End If
Next
End Sub

Note: You must have an entry in C4 before you start.
If the corresponding cell in column F is blank it will not show

regards
Derek

ps You can remove the first line of code. It was just to prove it was selecting expanding data.
 

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,023
Use an advanced filter (Data | Filter > Advanced Filter...) or create a PivotTable. If you go with the latter, put both column E and column F as row fields, and don't specify any data field.
Agent86 said:
I've been working on this one for about an hour now, but just can't figure out how to write the correct type of loop.

I have a list of names beginning in cell E20. This list will continue to expand and repeats many of the names. Beginning in cell F20 I have a second list of names that will continue to expand as well. All names in Column F are associated with the name on the same row in Column E.

I'd like to enter a name (from the list in Column E) in cell B5 and generate a new list which would begin in cell C5. This new list should be all the names from Column F whose cooresponding name in Column E matches what was entered in B5.

... I'm sorry if all that sounds confusing. Please let me know if clarification is needed. Thank you in advance fro any guidance you may be able to provide.
 

Agent86

Board Regular
Joined
Feb 28, 2003
Messages
229
Thank you both!

Derek, your method works fine. I was not approaching it correctly. I was trying to write a code that would insert a VLOOKUP formula and loop it.

tusharm, I thought about using a filter, but didn't want the name from column E to be listed on every line of the report. I never thought about a pivot table. I've not used those much, but I think that I'll explore your suggestion to see how that works.

Thank you again for your assistance.
 

Watch MrExcel Video

Forum statistics

Threads
1,113,859
Messages
5,544,706
Members
410,630
Latest member
Maggie28
Top