search loop

Hawjeen

New Member
Joined
Feb 23, 2018
Messages
23
Hi,

I need some help with creating a loop macro, hope someone would be so kind and help me

i need to search all cells in a range (b2:y200)
if a value (value would be a name - so text) is found, i would like to go to sheet ("data") find the value in range (B2:B100) and copy the cell to the right (Column A) and paste that value
instead of the name found initially.

and of course search for next value in range (b2:y200)

Does it make sense?

Regards
Hawjeen
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How about
Code:
Sub SearchLoop()

   Dim Cl As Range
   Dim Rng As Range
   Dim Qty As Long
   Dim i As Long
   Dim Dic As Object
   Dim ky As Variant
   
   Set Rng = Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Range("B2:Y200")
   Set Dic = CreateObject("scripting.dictionary")
   For Each Cl In Sheets("Data").Range("B2:B100")
      If Not Dic.exists(Cl.Value) Then Dic.Add Cl.Value, Cl.Offset(, -1).Value
   Next Cl
   For Each ky In Dic.keys
      Qty = Application.CountIf(Rng, ky)
      If Qty > 0 Then
         For i = 1 To Qty
            Rng.Find(ky, , , xlWhole, , , False, , False) = Dic(ky)
         Next i
      End If
   Next ky
End Sub
Change value in red to suit
 
Upvote 0
@Fluff
I am not familiar with Dictionary in VBA
Are you using Dictionary because it allows VBA to return EVERY match rather than only the first match?
 
Upvote 0
@Yongle
The idea of the dictionary is to put the values from the data sheet into memory & then loop through the dictionary to see if any of those values occur on the other sheet. It's a faster way of working than continually accessing the sheet.
For more info on Dictionaries look here https://excelmacromastery.com/vba-dictionary/
 
Upvote 0
Great!

If i want to modify it to paste it as a picture with a link, where do i need to change it?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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