Vba: look for a value scanning a row

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I have the range A1:A8 of sheet1 with a series of values.

In sheet2 I have a lot of rows, from B2:Z2 to B100:Z100

Scanning any single row from B to Z, I need to report in column AA which is the first of the values in sheet1 range A1:A8 that I meet (if any).

Can I have any suggestions?

Thank you.
 

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.
What exactly do you want to return to column AA? Do you want to return the value from A1:A8 which is found in B2:Z100 in Sheet2? Please clarify in detail.
 
Upvote 0
What exactly do you want to return to column AA? Do you want to return the value from A1:A8 which is found in B2:Z100 in Sheet2? Please clarify in detail.

Yes, I need the first value I meet among the reference values in range A1:A8 of sheet1.
 
Last edited:
Upvote 0
Non-VBA solution:


Book1
A
1a
2b
3c
4d
5e
6f
7g
8h
Sheet1



Book1
BCDEFGHIJKLMNOPQRSTUVWXYZAA
2wdeumaznxnaussxnqevxnvnuud
3wwwvskwwvtuwwtsruwwwiwwxsNot found
4gzqtaoxokuktekkevwvmdvaogg
5agqjrjscagyulsnozdftyrjoka
6ozzmquuzvtqlwrnfdbabvdckuf
7srzwrbkjhbbezjvnaxaicsgbhb
8zcjwwuxdbxeuesnxbausrxquyc
Sheet2
Cell Formulas
RangeFormula
AA2{=IFERROR(INDEX($B2:$Z2,MIN(IFERROR(MATCH(Sheet1!$A$1:$A$8,$B2:$Z2,0),26))),"Not found")}
Press CTRL+SHIFT+ENTER to enter array formulas.


WBD
 
Upvote 0
Try:
Code:
Sub FindVal()
    Application.ScreenUpdating = False
    Dim rng As Range, foundRng As Range
    For Each rng In Sheets("Sheet1").Range("A1:A8")
        Set foundRng = Sheets("Sheet2").Range("B2:Z100").Find(rng, LookIn:=xlValues, lookat:=xlWhole)
        If Not foundRng Is Nothing Then
            Sheets("Sheet1").Cells(Rows.Count, "AA").End(xlUp).Offset(1, 0) = foundRng
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
Code:
Sub FindVal()
    Application.ScreenUpdating = False
    Dim rng As Range, foundRng As Range
    For Each rng In Sheets("Sheet1").Range("A1:A8")
        Set foundRng = Sheets("Sheet2").Range("B2:Z100").Find(rng, LookIn:=xlValues, lookat:=xlWhole)
        If Not foundRng Is Nothing Then
            Sheets("Sheet1").Cells(Rows.Count, "AA").End(xlUp).Offset(1, 0) = foundRng
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub

No, something is wrong.

Taking a look at post #4 by @wideboydixon , the required outcome is definitely clearer.
 
Upvote 0
How does the macro not work for you? Please be specific.
 
Upvote 0
In your original post you said:
I need to report in column AA which is the first of the values in sheet1 range A1:A8
If not in column AA, where do you want to return the values?
 
Last edited:
Upvote 0
I think the logic is to look on each row for the first value that's in the list on sheet 1. Like this:

Code:
Public Sub FindByRow()


Dim thisRow As Long
Dim foundCol As Variant
Dim searchVal As Long
Dim minCol As Long


For thisRow = 2 To 100
    minCol = 99
    For searchVal = 1 To 8
        foundCol = Application.Match(Sheets("Sheet1").Cells(searchVal, "A").Value, Sheets("Sheet2").Range("B" & thisRow & ":Z" & thisRow), 0)
        If Not IsError(foundCol) Then
            If foundCol < minCol Then minCol = foundCol
        End If
    Next searchVal
    Sheets("Sheet2").Cells(thisRow, "AA").Value = IIf(minCol = 99, "Not found", Sheets("Sheet2").Cells(thisRow, minCol + 1).Value)
Next thisRow


End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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