Statement in Macro. If target cell contents start with a "z", then select target range and delete.

colmodoc

New Member
Joined
May 20, 2009
Messages
44
Office Version
  1. 365
Platform
  1. MacOS
Hi.
Looking for help on a macro please.
Example: Column A contains data. I need an statement which can delete the cell contents of a certain range say A2:A12 only if Cell A2 contents begins with the letter "z"
Thanks for viewing.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
will it always be a lowercase "z"?

Code:
Sub del()
If Left(Range("A2").Value, 1) = "z" Then Range("A2:A12").Delete
End Sub
 
Upvote 0
will it always be a lowercase "z"?

Code:
Sub del()
If Left(Range("A2").Value, 1) = "z" Then Range("A2:A12").Delete
End Sub

if not then

Code:
Sub del()
If Left(Ucase(Range("A2").Value), 1) = "Z" Then Range("A2:A12").Delete
End Sub
 
Upvote 0
Thank you Barry for your guidance on this.
Z will not always be lower case so thank you for the update above.
Additionally the starting point for occurrences of Z may not always be A2: Can the statement be altered to 'find the first occurrence of Z in ColumnA' and then to select from that point down to a defined cell (A135)?
 
Upvote 0
try

Code:
Sub del()
Dim rng As Range
For Each rng In Range("A2:A135")
If Left(UCase(rng.Value), 1) = "Z" Then Range(Cells(rng.Row, 1), Cells(135, 1)).Delete
Next
End Sub
 
Upvote 0
.. or more directly & less repetitively
Rich (BB code):
Sub ClearFromZ()
  Dim FirstZ As Range
  
  Set FirstZ = Range("A2:A135").Find(What:="z*", After:=Range("A135"), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlPrevious, MatchCase:=False)
  If Not FirstZ Is Nothing Then Range(FirstZ, Range("A135")).ClearContents
End Sub
 
Upvote 0
That's fantastic Barry and Peter.
And it does very much help.
Thank you both.
Colm
 
Upvote 0
You are very welcome. Thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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