Loop through a column, find a value, return offset value to cell

harts

Board Regular
Joined
Apr 5, 2011
Messages
111
Office Version
  1. 365
Platform
  1. Windows
I have been trying to find a solution to my problem. I find bits a pieces that kind of work but not a whole solution. I need to loop the names listed D2:AZ2, find a match in BH1:BH40, then return the value in BF1:BF40 to D3:AZ3. Thank you in advanced. It is much appreciated.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
try this code:
VBA Code:
Sub test()
Darray = Range("D2:az3")  ' load range D2 to Az3 into a variant array
Bfarray = Range("BF1:Bh40") ' load ranger  BF1 to Bh40 into a varaint array
 For i = 1 To UBound(Darray, 2)  ' loop through each column D to Az
  For j = 1 To UBound(Bfarray, 1) ' 2nd loop through each row 1 to 40
    If Darray(1, i) = Bfarray(j, 3) Then ' compare D2 etc to Bh1 etc
     Darray(2, i) = Bfarray(j, 1)        ' when match found copy to row 3
     Exit For
    End If
  Next j
 Next i
Range("D2:az3") = Darray  ' write the array back to worksheet
 
End Sub
 
Upvote 0
try this code:
VBA Code:
Sub test()
Darray = Range("D2:az3")  ' load range D2 to Az3 into a variant array
Bfarray = Range("BF1:Bh40") ' load ranger  BF1 to Bh40 into a varaint array
 For i = 1 To UBound(Darray, 2)  ' loop through each column D to Az
  For j = 1 To UBound(Bfarray, 1) ' 2nd loop through each row 1 to 40
    If Darray(1, i) = Bfarray(j, 3) Then ' compare D2 etc to Bh1 etc
     Darray(2, i) = Bfarray(j, 1)        ' when match found copy to row 3
     Exit For
    End If
  Next j
 Next i
Range("D2:az3") = Darray  ' write the array back to worksheet
 
End Sub
THAT WORKS PERFECT. THANK YOU FOR YOUR TIME.
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Another way to get the result

VBA Code:
Sub test2()
  With Range("D3:AZ3")
    .Formula = "=ifna(index($BF1:$BF40,match(D2,$BH1:$BH40,0)),"""")"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Both of these work well. Thank you. Thank Peter for the info on updating my information. I will do that. I modified both to do a 2nd look up but have 1 issue. I need to match the name in the 1st row with the name in column BP. Then return the value in column BS. Here is the part I have issues with the name in row1 and the name in column BP are not an exact match. The names in BP are mostly partials. The 1st works on all the cases where it is an exact match, but the 2nd works on the first 2 then stops after that.

VBA Code:
Sub test2()
  With Range("D4:AZ4")
    .Formula = "=ifna(index($BS1:$BS40,match(D1,$BP1:$BP40,0)),"""")"
    .Value = .Value
  End With
End Sub

or

Sub test()
Darray = Range("D1:az4")
Bfarray = Range("BP1:BS40")
For i = 1 To UBound(Darray, 1)
  For j = 1 To UBound(Bfarray, 1)
    If Darray(1, i) = Bfarray(j, 1) Then
     Darray(4, i) = Bfarray(j, 4)
     Exit For
    End If
  Next j
 Next i
Range("D1:az4") = Darray
 
End Sub
 
Last edited by a moderator:
Upvote 0
The reason my code stops after only two matches is because you changed one critical number: if you look at my original code:
VBA Code:
For i = 1 To UBound(Darray, 2)  ' loop through each column D to Az
you have changed this to:
VBA Code:
For i = 1 To UBound(Darray, 1)
The use of the ubound function is a way of looping through all elements of an array. The second parameter of the ubound function determines which dimension of the array it returns, so my line of code returns the 2nd dimension ( the parameter is set to 2) which in arrays relates to the columns, in your line of code it relates to the 1st dimension ( the paramter set to 1) which relates to rows, so your code only does 4 iterations because it is going from row 1 to row 4. Changing this back to my orginal code shoud solve the probelm of only two matches. However it doesn't solve the problem of the partila match which is a lot trickier
 
Upvote 0
I see, thank you for explaining that. I changed it and it does work correctly, except for the partial matches
The reason my code stops after only two matches is because you changed one critical number: if you look at my original code:
VBA Code:
For i = 1 To UBound(Darray, 2)  ' loop through each column D to Az
you have changed this to:
VBA Code:
For i = 1 To UBound(Darray, 1)
The use of the ubound function is a way of looping through all elements of an array. The second parameter of the ubound function determines which dimension of the array it returns, so my line of code returns the 2nd dimension ( the parameter is set to 2) which in arrays relates to the columns, in your line of code it relates to the 1st dimension ( the paramter set to 1) which relates to rows, so your code only does 4 iterations because it is going from row 1 to row 4. Changing this back to my orginal code shoud solve the probelm of only two matches. However it doesn't solve the problem of the partila match which is a lot trickier
 
Upvote 0
this will find a match with just the first word, it looks for a space so if there isn't a space it doesn't find a match, but you could combine the two.
VBA Code:
Sub test()
Darray = Range("D2:az3")  ' load range D2 to Az3 into a variant array
bfarray = Range("BF1:Bh40") ' load ranger  BF1 to Bh40 into a varaint array
 For i = 1 To UBound(Darray, 2) ' loop through each column D to Az
  Textb = Split(Darray(1, i), " ")
    If IsArray(Textb) Then
    For j = 1 To UBound(bfarray, 1) ' 2nd loop through each row 1 to 40
     Textg = Split(bfarray(j, 3), " ")
   
     If IsArray(Textg) Then
      If Textb(0) = Textg(0) Then ' compare the two first words
       Darray(2, i) = bfarray(j, 1)        ' when match found copy to row 3
       Exit For
      End If
     End If
    Next j
  ' you could add "else" here and then post the first code in here
   End If
 Next i
Range("D2:az3") = Darray  ' write the array back to worksheet
 
End Sub
 
Upvote 0
Thanks for updating your details. (y)

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time in post 5. 😊

Give this one a try

VBA Code:
Sub test3()
  With Range("D4:AZ4")
    .Formula = "=LET(bp,$BP1:$BP40,bs,$BS1:$BS40,XLOOKUP(D1,bp,bs,XLOOKUP(TEXTBEFORE(D1,"" "",,,1)&""*"",bp,bs,"""",2)))"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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