+ Reply to Thread
Results 1 to 4 of 4

Trouble deleting entire row fr a range based on text in one cell & value of 0 in another

Hybrid View

  1. #1
    Registered User
    Join Date
    04-16-2013
    Location
    Buffalo, NY
    MS-Off Ver
    Excel 2010
    Posts
    10

    Trouble deleting entire row fr a range based on text in one cell & value of 0 in another

    I am fairly new to writing macros. I have been using the following code to delete rows from my worksheet when the text in column L meets the criteria defined in the code. I found this code online and tweaked it to work for my macro:

    'This section of the code will delete the transfer to debenture loans from the file.
    
    Dim X As Long
    Dim Z As Long
    Dim LastRow As Long
    Dim FoundRowToDelete As Boolean
    Dim OriginalCalculationMode As Long
    Dim RowsToDelete As Range
    Dim SearchItems() As String
    
    Dim DataStartRow As Long
    Dim SearchColumn As String
    Dim SheetName As String
    
    DataStartRow = 1
    SearchColumn = "L"
    SheetName = "detail"
    
    SearchItems = Split("Tran to CPN,Settled,PIF", ",")
    
    On Error GoTo Whoops
    OriginalCalculationMode = Application.Calculation
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    
    With Worksheets(SheetName)
    LastRow = .Cells(.Rows.Count, SearchColumn).End(xlUp).Row
    For X = LastRow To DataStartRow Step -1
    FoundRowToDelete = False
    For Z = 0 To UBound(SearchItems)
    If InStr(.Cells(X, SearchColumn).Value, SearchItems(Z)) Then
    FoundRowToDelete = True
    Exit For
    End If
    
    Next
    
    If FoundRowToDelete Then
    If RowsToDelete Is Nothing Then
    Set RowsToDelete = .Cells(X, SearchColumn)
    Else
    Set RowsToDelete = Union(RowsToDelete, .Cells(X, SearchColumn))
    End If
    
    If RowsToDelete.Areas.Count > 100 Then
    RowsToDelete.EntireRow.Delete
    Set RowsToDelete = Nothing
    End If
    End If
    
    Next
    
    End With
    If Not RowsToDelete Is Nothing Then
    RowsToDelete.EntireRow.Delete
    End If
    
    Whoops:
    Application.Calculation = OriginalCalculationMode
    Application.ScreenUpdating = True
    This code has been running successfully - deleting the rows that I would expect it to delete.

    Now we need to add another criteria for consideration before deletion. The row should be deleted if the coumn L contains the text Tran to CPN, Settled, or PIF AND the value in column AF is 0 (zero - not blank). I tried to do a nesting if:

    With Worksheets(SheetName)
    LastRow = .Cells(.Rows.Count, SearchColumn).End(xlUp).Row
    For X = LastRow To DataStartRow Step -1
    FoundRowToDelete = False
    For Z = 0 To UBound(SearchItems)
    If InStr(.Cells(X, SearchColumn).Value, SearchItems(Z)) And .Cells(X, "AF").Value = "=0" Then
    FoundRowToDelete = True
    Exit For
    End If
    Now the rows are not deleting. Cell AF contains a formula. The results are truly 0 - not just rounding to 0. I even tested it by typing in a 0 in one of the cells rather than the formula and the macro still didn't delete the row. I'm not getting an error in VBA - it is running. It just isn't returning the desired results.

    Please let me know if any additional information would be helpful. Thank you in advance for your help!!!

    Kelly

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Trouble deleting entire row fr a range based on text in one cell & value of 0 in anoth

    If InStr(.Cells(X, SearchColumn).Value, SearchItems(Z)) And .Cells(X, "AF").Value < 0.01 Then
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    04-16-2013
    Location
    Buffalo, NY
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Trouble deleting entire row fr a range based on text in one cell & value of 0 in anoth

    Quote Originally Posted by patel45 View Post
    If InStr(.Cells(X, SearchColumn).Value, SearchItems(Z)) And .Cells(X, "AF").Value < 0.01 Then
    Thank you for the quick reply. My only concern with that suggestion is that I think it would delete rows with a value less than 0. We could have values in that column less than 0 that I would not want to delete.

  4. #4
    Registered User
    Join Date
    04-16-2013
    Location
    Buffalo, NY
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Trouble deleting entire row fr a range based on text in one cell & value of 0 in anoth

    Quote Originally Posted by kmarshall6576 View Post
    Thank you for the quick reply. My only concern with that suggestion is that I think it would delete rows with a value less than 0. We could have values in that column less than 0 that I would not want to delete.
    I changed your < 0.01 to = 0 and it worked! THANK YOU SO MUCH!!!! I'll mark this solved!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1