Check for first 2 charecters in cell if not found delete the row.

flay

New Member
Joined
Feb 9, 2021
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hey.
I have this script which checks and replaces first 2 characters in a cell(total 4 strings for it check for).
I want to add that IF the cell does not start with one of these strings then it should delete the row completely.
Here is the code:(The strings im checking for are marked)
Sub Change_Replace()
Dim Ary As Variant
Dim i As Long

Ary = Array("NI", "801", "RE", "821", "NV", "571", "NF", "831")
For i = 0 To UBound(Ary) Step 2
With Range("A2", Range("A" & Rows.Count).End(xlUp))
.Value = Evaluate(Replace("if(left(@,2)=""" & Ary(i) & """," & Ary(i + 1) & "&mid(@,3,100),@)", "@", .Address))
End With
Next i
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi,

Use below script:
VBA Code:
Sub Change_Replace()
    Dim Ary As Variant
    Dim i As Long
   
    Ary = Array("NI", "801", "RE", "821", "NV", "571", "NF", "831")
    For i = 0 To UBound(Ary) Step 2
        For rowno = 2 To Range("A" & Rows.Count).End(xlUp).Row
            If Left(Cells(rowno, 1), Len(Ary(i))) = Ary(i) Then
                Cells(rowno, 1).Delete
            End If
        Next
    Next i
End Sub
 
Upvote 0
Another option
VBA Code:
Sub flay()
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array("NI", "801", "RE", "821", "NV", "571", "NF", "831")
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if((left(@,2)<>""NI"")*(left(@,2)<>""RE"")*(left(@,2)<>""NV"")*(left(@,2)<>""NF""),"""",@)", "@", .Address))
      .SpecialCells(xlBlanks).EntireRow.Delete
   End With

   For i = 0 To UBound(Ary) Step 2
      With Range("A2", Range("A" & Rows.Count).End(xlUp))
         .Value = Evaluate(Replace("if(left(@,2)=""" & Ary(i) & """," & Ary(i + 1) & "&mid(@,3,100),@)", "@", .Address))
      End With
   Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

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