Delete Cell if 3rd character not equal to...

Pumper

Board Regular
Joined
Sep 12, 2013
Messages
114
Office Version
  1. 365
Hi All,

I am attempting to run some vba over multiple columns to delete any cell where the 3rd character does not equal any of the letters in Cell 1A.

The length of the original column can vary.

The letters would be different across row 1 for each additional column but I should be able to update the code if I can get the first column to work.

Any assistance/advice on this would be greatly appreciated!

So Original Data for example would be something like this:

1725337742500.png

The desired outcome would be this:

1725337782942.png


Thanks
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
try this:
PHP:
Option Explicit
Sub del()
Dim lr&, i&, k&, rng, res()
lr = Cells(Rows.Count, "A").End(xlUp).Row ' last used row of column A
rng = Range("A2:A" & lr).Value  'store data into array
ReDim res(1 To UBound(rng), 1 To 1)
For i = 1 To UBound(rng)
    'search for 3rd letter in cell A1 
    If InStr(1, Cells(1, "A") & ",", Mid(rng(i, 1), 3, 1) & ",") Then ' if found
        k = k + 1: res(k, 1) = rng(i, 1) ' store results into array
    End If
Next
If k > 0 Then
    Range("A2:A1000").ClearContents 'delete previous results
    Range("A2").Resize(k, 1).Value = res ' paste results back to sheet
End If
End Sub
 
Upvote 1
Solution
try this:
PHP:
Option Explicit
Sub del()
Dim lr&, i&, k&, rng, res()
lr = Cells(Rows.Count, "A").End(xlUp).Row ' last used row of column A
rng = Range("A2:A" & lr).Value  'store data into array
ReDim res(1 To UBound(rng), 1 To 1)
For i = 1 To UBound(rng)
    'search for 3rd letter in cell A1
    If InStr(1, Cells(1, "A") & ",", Mid(rng(i, 1), 3, 1) & ",") Then ' if found
        k = k + 1: res(k, 1) = rng(i, 1) ' store results into array
    End If
Next
If k > 0 Then
    Range("A2:A1000").ClearContents 'delete previous results
    Range("A2").Resize(k, 1).Value = res ' paste results back to sheet
End If
End Sub
Brilliant! works a treat, I would never have come up with that...

Thank you so much for your time (y)
 
Upvote 0
An option without looping through each value ..

VBA Code:
Sub Match_Third()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(match(""*""&mid(#,3,1)&""*"",A1,0)),#,0)", "#", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
    On Error GoTo 0
  End With
End Sub
 
Upvote 0
An option without looping through each value ..

VBA Code:
Sub Match_Third()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(match(""*""&mid(#,3,1)&""*"",A1,0)),#,0)", "#", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
    On Error GoTo 0
  End With
End Sub
Hi Peter, sorry for the late reply.

Just tried that and worked perfectly, very nice!

Thank you for taking the time to look at this 👍
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0
I have just noticed that there are the odd few that are actually 5 characters long so the "third character" will not always be correct, is there a way to adjust this code from Peter to do the same but using the second last character as the variable rather than a strict 3rd character?

Sorry to have only noticed this now...
 
Upvote 0
, is there a way to adjust this code from Peter to do the same but using the second last character as the variable rather than a strict 3rd character?
That is not much of a change so try this one

VBA Code:
Sub Match_2nd_Last()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(match(""*""&left(right(#,2),1)&""*"",A1,0)),#,0)", "#", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
    On Error GoTo 0
  End With
End Sub
 
Upvote 0
That is not much of a change so try this one

VBA Code:
Sub Match_2nd_Last()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(match(""*""&left(right(#,2),1)&""*"",A1,0)),#,0)", "#", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlNumbers).Delete Shift:=xlUp
    On Error GoTo 0
  End With
End Sub
I would hate to admit how long I tried to solve that :sneaky:

You are a champion Peter! THANK YOU.
 
Upvote 0

Forum statistics

Threads
1,223,753
Messages
6,174,307
Members
452,554
Latest member
Louis1225

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