Search for Variable in Array

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
590
Office Version
  1. 2019
Platform
  1. Windows
I am running a loop, where i go down a list of part numbers, and i need to check each number against a variable list, and if they are in there, it returns yes, and if not, returns a no.

VBA Code:
Sheets("77 surplus").Select
                  
    Dim arr As Variant
                  
        arr = Range("I2:I122")
              
        finalrow = Cells(Rows.Count, 1).End(xlUp).Row
        
            For i = 2 To finalrow
        
            Cells(i, 1).Value = partNumber
            
            '**if partNumber variable exists in the list of numbers in arr then**
                'cells(i,4).value = "Y"
            'else
                'cells(i,4).value = "N"
            'End If
                
            Next i

How can i search that list in the array?
 
i am getting this pop up when i try to run the script

1575669478149.png
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try
VBA Code:
For i = 2 To finalrow
    '.Cells(i, 1).Value = partNumber: Rem I'm not sure what this line is about
    If IsNumeric(Application.Match(.Cells(i,1).Value, arr,0 )) Then
      .cells(i,4).value = "Y"
    Else
      .Cells(i,4.Value) = "N"
    End If
Next i

Or

Code:
With Range("A:A")
    With Range(.Cells(1,1), .Cells(Rows.Count,1).End(xlUp))
        With .Offset(0, 3)
            .FormulaR1C1 = "=IF(ISNUMBER(MATCH(RC1, R2C9:R122C9,0), "Y", "N")
            .Value = .Value
        End With
    End With
End With
 
Upvote 0
That looks more like you're antivirus/security kicking in, rather than a problem with Xl.
Can you edit it's settings?
 
Upvote 0
Try
VBA Code:
For i = 2 To finalrow
    '.Cells(i, 1).Value = partNumber: Rem I'm not sure what this line is about
    If IsNumeric(Application.Match(.Cells(i,1).Value, arr,0 )) Then
      .cells(i,4).value = "Y"
    Else
      .Cells(i,4.Value) = "N"
    End If
Next i

Or

Code:
With Range("A:A")
    With Range(.Cells(1,1), .Cells(Rows.Count,1).End(xlUp))
        With .Offset(0, 3)
            .FormulaR1C1 = "=IF(ISNUMBER(MATCH(RC1, R2C9:R122C9,0), "Y", "N")
            .Value = .Value
        End With
    End With
End With


Option A worked,

thanks,
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,483
Members
448,967
Latest member
visheshkotha

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