Consulting

Results 1 to 7 of 7

Thread: Delete Row if Cell Contents = "ABC"

  1. #1
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location

    Delete Row if Cell Contents = "ABC"

    Hi

    I am trying to write a wee macro that will check the contents of a cell and delete a row if certain criteria met.

    I have a bit of code:

    [VBA]Sub DeleteRow()
    ' Macro Recorded 17/11/2009 by Gerry McNally
    ' Check Column E for particular letters - if found then delete row
    Dim iLastRow As Long
    Dim i As Long

    Application.ScreenUpdating = False
    iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Dim Rng As Range

    For i = iLastRow To 1 Step -1
    If LCase(Left(Cells(i, "K").Value, 1)) = "CBC" Then
    Rows(i).Delete
    End If

    Next i

    Application.ScreenUpdating = True

    End Sub[/VBA]


    But not only does it not work, but to be honest I was changing things to suit anyway. In the instance above I had created a column (K) that read from Column E the first 3 characters and I tried to creat the macro to check column K and delete a row if it contained "CBC"


    But what I would rather do is have a macro thaty would just check column E and if the first 3 letters = either CBC or MBC or SBC then delete the whole row.

    Thanks for any help

    Hoopsah
    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    The posted length is off. Try
    [VBA]If LCase(Left(Cells(i, "K").Value, 3)) = "CBC" Then[/VBA]
    or you could use Like
    [VBA]If LCase(Cells(i,"K").Value) Like "CBC*" Then[/VBA]

  3. #3
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location
    Fraid those changes still do not delete any rows

    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Right. We were both comparing LCase to "CBC". UCase would be more appropriate.

  5. #5
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location
    Man!!!

    Thank you, it is now deleting the rows ok - I can only say in my defence that sometimes I anm quite thick!!

    Cheers Dude

    Hoopsah
    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Use Option Compare Text to avoid the need for UCase/LCase operations.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location
    Cheers md

    I didn't know about that option but checked it out on ozgrid and it does make ot a lot easier to check

    Thanks

    Hoops
    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •