Consulting

Results 1 to 6 of 6

Thread: HELP macro to Delete Row based on two cells

  1. #1

    HELP macro to Delete Row based on two cells

    I am trying to write some code that will compare two cells on a row and if they match then it will delete the row.

    Column A-has the number 1, 2, 3, 4, or 5 entered
    Column B-has either a date or is blank

    I need the macro to look at Column A and Column B and for example if the cell A2 has a 3, 4, or 5 and B2 is blank the macro will delete that row.
    The spreadsheet will vary on how many rows it contains. It can contain up to 5,000 rows and the macro would need to go through all the rows.
    Any help would be greatly appreciated. Thanks

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    You could try something like this

    [VBA]Sub Deleter()
    Dim MyRange As Range, rCell As Range
    Dim Stopper As Integer


    Stopper = WorksheetFunction.CountA(Range("A:A"))

    Set MyRange = Range("A1:A" & Stopper)

    For Each rCell In MyRange.Cells

    If rCell.Value > 2 Then If rCell.Offset(, 1).Value = "" Then rCell.EntireRow.Delete xlUp

    Next rCell


    End Sub[/VBA]

    hope this helps

  3. #3
    I tried using this code and it does not appear to work. Any other thoughts?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub ProcessData()
    Dim i As Long
    Dim LastRow As Long

    Application.ScreenUpdating = False

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = LastRow To 1 Step -1

    If .Cells(i, "A").Value >= 2 And .Cells(i, "B").Value = "" Then

    .Rows(i).delee
    End If
    Next i

    End With

    Application.ScreenUpdating = True

    End Sub
    [/vba]
    ____________________________________________
    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

  5. #5
    Well I figured out why the previous to code examples were not working I have included a correction as to what I am needing.

    I am trying to write some code that will compare two cells on a row and if they match then it will delete the row.

    Column A-has the number 1, 2, 3, 4, or 5 entered
    Column B-has either a date or is blank

    I need the macro to look at Column A and Column B and for example if the cell A2 has a 3, 4, or 5 and B2 has a date the macro will delete that row.
    The spreadsheet will vary on how many rows it contains. It can contain up to 5,000 rows and the macro would need to go through all the rows.
    Any help would be greatly appreciated. Thanks

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]


    Public Sub ProcessData()
    Dim i As Long
    Dim LastRow As Long

    Application.ScreenUpdating = False

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = LastRow To 1 Step -1

    If .Cells(i, "A").Value >= 2 And IsDate(.Cells(i, "B").Value) Then

    .Rows(i).delee
    End If
    Next i

    End With

    Application.ScreenUpdating = True

    End Sub
    [/vba]
    ____________________________________________
    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

Posting Permissions

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