Consulting

Results 1 to 6 of 6

Thread: Urgent VBA Help Required!!

  1. #1

    Urgent VBA Help Required!!

    Hi,

    I need a macro to do the following function :

    Check the data in one column. If that data equals 0 then look in a different column containing new data and check to see if that data equals the line above or the line below.
    If no then leave.
    If yes then delete the line that contained the 0 in the first column checked.

    I have to date this:

    Code:
    [VBA]Option Explicit

    Sub DeleteRows()
    Dim LastRow As Long
    Dim i As Long

    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = LastRow To 2 Step -1
    If Cells(i, "A").Value = 0 Then
    If Cells(i - 1, "B").Value = Cells(i, "B").Value Or _
    Cells(i + 1, "B").Value = Cells(i, "B").Value Then
    Cells(i, "A").EntireRow.Delete
    End If
    End If
    Next i
    End Sub[/VBA]

    Unfortunately, this isn't working, as the lines aren't being deleted, and maybe not even identified as incorrect.

    Please, Please, Please can someone help!!

    Thanks,
    Last edited by Bob Phillips; 09-30-2010 at 09:14 AM. Reason: Added VBA tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What's wrong with it, it looks fine to me? How does it not work?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Sep 2010
    Posts
    28
    Location
    I've just tested your code in my created sheet, worked great.
    When it comes to deletion loops, the first thing I suspect is logic, but your code already made wisely and runs backwards on the lines, so the line indexes remain correct.

    I wanna believe you have experience with vba, but if not: Did you put the code inside the specific sheet's object on VB Editor?
    You must put it there and not in "ThisWorkbook" or in a module, since the code doesn't have any reference to the desired sheet.

    When you check it, please update us here if it was the issue or not.

    Hope I've helped,
    Meir.

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Quote Originally Posted by MeiR_ct
    ..... Did you put the code inside the specific sheet's object on VB Editor?
    You must put it there and not in "ThisWorkbook" or in a module, since the code doesn't have any reference to the desired sheet.

    When you check it, please update us here if it was the issue or not.

    Hope I've helped,
    Meir.
    Meir, sorry but your instruction is incorrect, the code is NOT worksheet event code it should reside in a standard module (Alt+F11, I, M) and the code does work as you state, if Willo is still having a problem he/she will have to upload a workbook that they are having trouble with
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    VBAX Regular
    Joined
    Sep 2010
    Posts
    28
    Location
    Heh, okay.
    As you can see in my stats, I'm a beginner and not a helper.
    I'll suggest myself to keep learning and gaining some more experience, before helping others

  6. #6
    VBAX Tutor mohanvijay's Avatar
    Joined
    Aug 2010
    Location
    MADURAI
    Posts
    268
    Location
    i think there is some upper or lower case problem or cells data may contain spaces try this

    If UCase(Trim(Cells(i - 1, "B").Value)) = UCase(Trim(Cells(i, "B").Value)) Or _
                UCase(Trim(Cells(i + 1, "B").Value)) = UCase(Trim(Cells(i, "B").Value)) Then
                    Cells(i, "A").EntireRow.Delete
                End If

Posting Permissions

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