+ Reply to Thread
Results 1 to 4 of 4

Trying to create a macro to delete entire rows based on a helper column having an account#

Hybrid View

  1. #1
    Registered User
    Join Date
    04-18-2012
    Location
    indiana, usa
    MS-Off Ver
    Excel 2010
    Posts
    35

    Trying to create a macro to delete entire rows based on a helper column having an account#

    I have a macro setup to create a helper column and paste a vlookup formula to return wether or not an account number matches on another sheet or not. Now i need the macro to delete the rows that have the matching account numbers but keep the rows that have a n/a where it doesn't match. ANy help?

        Range("AB6").Select
        ActiveCell.FormulaR1C1 = _
            "=VLOOKUP(RC[-27],Delinquency_Report.xls!R7C3:R2133C3,1,FALSE)"
        Selection.AutoFill Destination:=Range("AB6:AB557"), Type:=xlFillDefault
        Range("AB6:AB557").Select
    End Sub

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Trying to create a macro to delete entire rows based on a helper column having an acco

    Look at notes throughout the code. I could not tell what the result of the VLOOKUP is. If the result is a 1/0 (True/False), or simply when the acct # matches the acct# is returned, and assuming the acct# is in fact a number and not an alphanumeric string, the following should suffice.

    Sub DeleteRows()
    
    Dim rngCell As Range
    Dim rngSource As Range
    
       With Application
          .ScreenUpdating = False
          .EnableEvents = False
          .Calculation = xlCalculationManual
       End With
    
          'You will need to change the sheet name to match your workbook below
          With Sheets("Sheet1")
             'Dynamically assuming no blanks in range
             Set rngSource = .Range("AB6", .Range("AB6").End(xlDown))
             'Or static based on original post; De-comment to use
             'Set rngSource = .Range("AB6:AB557")
          End With
          
          For Each rngCell In rngSource
             'Might need to change the logic below; could not tell from OP
             If rngCell.Value > 0 Then
                rngCell.EntireRow.Delete
             End If
          Next
    
       With Application
          .EnableEvents = True
          .ScreenUpdating = True
          .Calculation = xlCalculationAutomatic
       End With
    
    End Sub
    Last edited by AlvaroSiza; 04-19-2012 at 10:41 AM.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Registered User
    Join Date
    04-18-2012
    Location
    indiana, usa
    MS-Off Ver
    Excel 2010
    Posts
    35

    Re: Trying to create a macro to delete entire rows based on a helper column having an acco

    The cell format for the helper column is set to general. If a result is matched it shows the account #, other wise it has the #n/a thing. Does this help? WIth the code you gave me it would still not work, . Any ideas what I need to do, I just want it to delelete any row that doesnt end with "#N/A"

    Quote Originally Posted by AlvaroSiza View Post
    Look at notes throughout the code. I could not tell what the result of the VLOOKUP is. If the result is a 1/0 (True/False), or simply when the acct # matches the acct# is returned, and assuming the acct# is in fact a number and not an alphanumeric string, the following should suffice.

    Sub DeleteRows()
    
    Dim rngCell As Range
    Dim rngSource As Range
    
       With Application
          .ScreenUpdating = False
          .EnableEvents = False
          .Calculation = xlCalculationManual
       End With
    
          'You will need to change the sheet name to match your workbook below
          With Sheets("Sheet1")
             'Dynamically assuming no blanks in range
             Set rngSource = .Range("AB6", .Range("AB6").End(xlDown))
             'Or static based on original post; De-comment to use
             'Set rngSource = .Range("AB6:AB557")
          End With
          
          For Each rngCell In rngSource
             'Might need to change the logic below; could not tell from OP
             If rngCell.Value > 0 Then
                rngCell.EntireRow.Delete
             End If
          Next
    
       With Application
          .EnableEvents = True
          .ScreenUpdating = True
          .Calculation = xlCalculationAutomatic
       End With
    
    End Sub

  4. #4
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Trying to create a macro to delete entire rows based on a helper column having an acco

    Try changing this line

     If rngCell.Value > 0 Then
    to
    If Not IsError(rngCell.Value) Then
    Please be as detailed as possible when stating that something 'does not work'. Were there error pop-ups? What was the description of the error? Etc.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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