deleting a cell based on character length?

  • Thread starter Thread starter Marcusdmc
  • Start date Start date
M

Marcusdmc

if I wanted to delete a cell in my selected row that was shorter than
say 16 characters, how would I create a macro to do that?

I would be checking for LEN(A:A) < 16 then delete?

Thanks for any help!

-Marcus
 
To clear the cell contents of anything with <16 characters in Column A, try
something like this:

Sub test()
Dim c As Range
For Each c In Application.Intersect(ActiveSheet.UsedRange,
ActiveSheet.Range("A:A"))
If Len(c.Value) < 16 Then
c.Clear
End If
Next c
End Sub

If you want to delete the entire row instead of just clearing the cell, then
something like this:

Sub test2()
Dim c As Range
Dim rngDelete As Range
For Each c In Application.Intersect(ActiveSheet.UsedRange,
ActiveSheet.Range("A:A"))
If Len(c.Value) < 16 Then
If rngDelete Is Nothing Then
Set rngDelete = c
Else
Set rngDelete = Application.Union(c, rngDelete)
End If
End If
Next c
If Not rngDelete Is Nothing Then rngDelete.EntireRow.Delete

End Sub
 
To clear the cell contents of anything with <16 characters in Column A, try
something like this:

Sub test()
Dim c As Range
For Each c In Application.Intersect(ActiveSheet.UsedRange,
ActiveSheet.Range("A:A"))
If Len(c.Value) < 16 Then
c.Clear
End If
Next c
End Sub

If you want to delete the entire row instead of just clearing the cell, then
something like this:

Sub test2()
Dim c As Range
Dim rngDelete As Range
For Each c In Application.Intersect(ActiveSheet.UsedRange,
ActiveSheet.Range("A:A"))
If Len(c.Value) < 16 Then
If rngDelete Is Nothing Then
Set rngDelete = c
Else
Set rngDelete = Application.Union(c, rngDelete)
End If
End If
Next c
If Not rngDelete Is Nothing Then rngDelete.EntireRow.Delete

End Sub

--
Hope that helps.

Vergel Adriano








- Show quoted text -

Thanks for the direction! Turned off screen updating to make it go
faster!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top