1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

VBA Deleting row based on cell value

Discussion in 'Business Applications' started by JackAndCoke, Jul 14, 2011.

Thread Status:
Not open for further replies.
  1. JackAndCoke

    JackAndCoke Thread Starter

    Joined:
    Apr 26, 2007
    Messages:
    152
    I have a fairly simple task. From Row 17 to row 51 and Row 96 to 163 , I want to delete the row if cells P and Q are empty.

    From row 57 to row 94 I want to delete the row if cells B, C, P, and Q are empty

    I'm not sure what I'm doing wrong here.

    If you see something glaringly wrong or know a quicker way to do this, I would be very thankful.
     
  2. JackAndCoke

    JackAndCoke Thread Starter

    Joined:
    Apr 26, 2007
    Messages:
    152
    Here's an example work sheet. If the code works right in the example, it would delete rows 4, 5, 9, and 10.


    Code:
    Dim endrow As Long
    Dim x As Long
    
    For x = endrow To 29 Step -1
    If Sheets("Sheet1").Range("P" & x).Value = "" And Sheets("Sheet1").Range("Q" & x).Value = "" Then
    Sheets("Sheet1").Rows(x).EntireRow.Delete
    End If
    
    Next x
    Why won't it remove the rows?
     

    Attached Files:

  3. Jimmy the Hand

    Jimmy the Hand

    Joined:
    Jul 28, 2006
    Messages:
    1,223
    Well, this one seems certainly wrong:
    Code:
    Set endrow = Sheets("Work Order").Range("A17").End(xlUp).Row
    Should be:
    Code:
    Set endrow = Sheets("Work Order").Range("A17").End(xlDown).Row
    Also, instead of
    Code:
    Selection.delete shift:=x1Up
    this one looks better (note the digit "1" in x1Up replaced by the letter "L"):
    Code:
    Selection.delete shift:=xlUp
    Although it's actually useless in our case, because entire rows are deleted.

    And here's a quicker way (no loops):
    Code:
    Sub CleanUp()
        Dim Rng As Range
        
        Set Rng = Intersect(Range("96:163"), _
            Range("P:P").SpecialCells(xlCellTypeBlanks).EntireRow, _
            Range("Q:Q").SpecialCells(xlCellTypeBlanks).EntireRow)
        If Not Rng Is Nothing Then Rng.Delete
        
        Set Rng = Intersect(Range("57:94"), _
            Range("B:B").SpecialCells(xlCellTypeBlanks).EntireRow, _
            Range("C:C").SpecialCells(xlCellTypeBlanks).EntireRow, _
            Range("P:P").SpecialCells(xlCellTypeBlanks).EntireRow, _
            Range("Q:Q").SpecialCells(xlCellTypeBlanks).EntireRow)
        If Not Rng Is Nothing Then Rng.Delete
        
        Set Rng = Intersect(Range("17:51"), _
            Range("P:P").SpecialCells(xlCellTypeBlanks).EntireRow, _
            Range("Q:Q").SpecialCells(xlCellTypeBlanks).EntireRow)
        If Not Rng Is Nothing Then Rng.Delete
    End Sub
    I think the code is self-explanatory, but should you have questions, ask anyway.

    Jimmy
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1007326

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice