How to delete a cell that doesn't have an "a" or "b" at the end of the value?

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I know how to use a formula such as =IF(RIGHT(C1,1)="a",TRUE,FALSE), but how do I go about incorporating that into VBA?

I'm trying to look at column D, a cell value that doesn't end with an "a" or "b" should be deleted. BTW, my data varies, and so I use LR quite a bit as can't assume the last row is always going to be the same.

Has_Has_Not_Taken_Report_04282022.xlsx.xls
ABCDE
1619155M5BASE8787ASE
2619155M6BASE8282a
3619155M7BASE8383a
4619156BASE8080
5619158BASE8484a
6VCIWB01SSFIA22ASE8282b
7ASE8383b
8ASE8484b
Working


The result would look like -
Has_Has_Not_Taken_Report_04282022.xlsx.xls
ABCDE
1619155M5BASE87ASE
2619155M6BASE8282a
3619155M7BASE8383a
4619156BASE80
5619158BASE8484a
6VCIWB01SSFIA22ASE8282b
7ASE8383b
8ASE8484b
Working


Thank you
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
There's a number of different ways you could do this with VBA, here's just one (assumes the code is run from the workbook with the data)

VBA Code:
Option Explicit
Sub keep_AB()
    Dim ws As Worksheet, i As Long, a
    Set ws = Worksheets("Working")
    a = ws.Range("D1", ws.Cells(Rows.Count, "D").End(xlUp))
    
    For i = 1 To UBound(a)
        If Not a(i, 1) Like "*a" And Not a(i, 1) Like ("*b") Then _
        a(i, 1) = vbNullString
    Next i
    ws.Range("D1").Resize(UBound(a)).Value = a
End Sub
 
Upvote 0
Solution
There's a number of different ways you could do this with VBA, here's just one (assumes the code is run from the workbook with the data)

VBA Code:
Option Explicit
Sub keep_AB()
    Dim ws As Worksheet, i As Long, a
    Set ws = Worksheets("Working")
    a = ws.Range("D1", ws.Cells(Rows.Count, "D").End(xlUp))
   
    For i = 1 To UBound(a)
        If Not a(i, 1) Like "*a" And Not a(i, 1) Like ("*b") Then _
        a(i, 1) = vbNullString
    Next i
    ws.Range("D1").Resize(UBound(a)).Value = a
End Sub
Worked perfectly, thank you. Now, I get to try and figure out what exactly is happening :)
 
Upvote 0
One more variant

VBA Code:
Sub jec()
Range("D1", Range("D" & Rows.Count).End(xlUp)).Name = "ar"
[ar] = [if((right(ar)<>"a")*(right(ar)<>"b"),"",ar)]
End Sub

Or (same idea):

VBA Code:
Sub jec()
 With Range("D1", Range("D" & Rows.Count).End(xlUp))
   .Value = Evaluate(Replace("if((right(#)<>""a"")*(right(#)<>""b""),"""",#)", "#", .Address))
 End With
End Sub
 
Upvote 0
Worked perfectly, thank you. Now, I get to try and figure out what exactly is happening :)
Glad we could help and thanks for the feedback :) Personally, I would look at one of @JEC 's suggestions - the less code the better ;)
 
Upvote 0
the less code the better
I'm not suggesting anything at all is wrong with @JEC's codes here, but as a general rule I don't think that is a good way to judge a code.

I don't know how representative the original sample data is but based on that alone, it looks like an alternative approach would be "if the value is numeric then clear it". If that was the case then another along the lines of JEC's code might be

VBA Code:
Sub ClearCells()
  With Range("D1", Range("D" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(#),"""",#)", "#", .Address))
  End With
End Sub
 
Upvote 0
In general, to delete all non-value in column D:
VBA Code:
Range("D:D").SpecialCells(xlCellTypeConstants, xlNumbers).ClearContents
 
Upvote 0
I'm not suggesting anything at all is wrong with @JEC's codes here, but as a general rule I don't think that is a good way to judge a code.

I don't know how representative the original sample data is but based on that alone, it looks like an alternative approach would be "if the value is numeric then clear it". If that was the case then another along the lines of JEC's code might be

VBA Code:
Sub ClearCells()
  With Range("D1", Range("D" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(isnumber(#),"""",#)", "#", .Address))
  End With
End Sub
Yes Peter, you're quite right :)
 
Upvote 0
VBA Code:
Range("D:D").SpecialCells(xlCellTypeConstants, xlNumbers).ClearContents
Only with that, to avoid a possible code error, you would need to use an 'on error' or else first check that there are some such values to clear.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
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