Search each cell in range for match in an array

spidaman

Board Regular
Joined
Jul 26, 2015
Messages
116
Office Version
  1. 365
Platform
  1. Windows
Can anyone help find a fast solution for finding an Instr match for each cell in a range from an array please?

The code below indicates what I'm trying to do, but I know it currently won't search against each value in myArray.

VBA Code:
myArray = UKP.Worksheets("lookup").Range("A1:A30").Value

If frmTest.RmBox.Value = True Then
        For Each cel In Hyprange
              If InStr(cel.Value, myArray) > 0 Then
                cel.EntireRow.Delete
              End If
        Next
End If

How can I adjust the Instr formula to search against each value in the array?

Any advice on improvement for the look-up speed also very welcome.

Many thanks in advance.
 
Am I correct in thinking that using Union method here creates a range of all the rows with matches to values in the array, allowing deletion altogether?
Yes you are :)
When you get the 1st match Rng is Nothing so you set Rng as the Cel, after which you can use Union.
If you try to do a Union when Rng is Nothing you will get an error.
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
The rng variable allows the Union parameters to expand on each iteration of the loop. You cannot set the rng to a specific range prior to running the loop because you don't know where the matched items reside.
 
Upvote 0
How about using
VBA Code:
IF IsNumeric(Application.Match("*"&cell.Value&"*", myArray, 0)) Then
instead of looping through myArray with InStr.
 
Upvote 0
The rng variable allows the Union parameters to expand on each iteration of the loop. You cannot set the rng to a specific range prior to running the loop because you don't know where the matched items reside.
One method might be to give Rng a value way outside of what the loop might add and then stripping that of at the end

VBA Code:
Set rng = Range("AA9999")

For each cell in HypRange
    If something then Set rng = Application.Union(cell, rng)
Next cell

Set rng Application.Instersect(rng, Range("A:Z")
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,792
Members
449,048
Latest member
greyangel23

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