+ Reply to Thread
Results 1 to 12 of 12

Help with macro to delete rows based on values

  1. #1
    Forum Contributor
    Join Date
    11-25-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    107

    Help with macro to delete rows based on values

    Hi,

    I have a following code and I need help fixing it.

    First of all I have 5 sheets and I'm wondering if it can be adjusted so that the first part of formula goes thru 5 sheets at once rather them me changing the sheet a repeating the formula again.

    in the 2nd part of the formula i'm wondering how it can be adjusted so that It can delete other valuer rather then just value "50"
    for example values like" 50, 30, 25".

    I also need a 3rd part which would be formula to go thru column c and delete rows if they have various values.

    I would appreciate if someone can help me out with this and most important is to keep the formula simple so I can understand what's going on,
    or maybe explain how it was done.

    Thank you very much.


    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Hope this help!

    PHP Code: 
    Sub GPE_COM()
     
    Dim X As Longlast_Row As Long
     Dim dRng 
    As Range
        
     Sheets
    ("ARP").Select   'SHEET ARP'
     
    last_Row Range("A80536").End(xlUp).Row
     
    For last_Row To 1 Step -1
        
    If Range("D" X).Value CDate("01/01/2011") Or Range("A" X).Value 50 Then '...'
            
    If dRng Is Nothing Then
                Set dRng 
    Range("D" X).EntireRow
            
    Else
                
    Set dRng Union(dRngRange("D" X).EntireRow)
            
    End If
        
    End If
     
    Next X
     
     
    If Not dRng Is Nothing Then dRng.Delete
      
    End Sub 

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Help with macro to delete rows based on values

    Quote Originally Posted by jimstrongy View Post
    ....
    for example values like" 50, 30, 25".
    ...
    Please Login or Register  to view this content.
    Try this
    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Help with macro to delete rows based on values

    Hi jimstrongy,

    Try this (initally on a copy of your data in case the results are not as expected):

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  5. #5
    Forum Contributor
    Join Date
    11-25-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    107

    Re: Hope this help!

    Quote Originally Posted by Sa DQ View Post
    PHP Code: 
    Sub GPE_COM()
     
    Dim X As Longlast_Row As Long
     Dim dRng 
    As Range
        
     Sheets
    ("ARP").Select   'SHEET ARP'
     
    last_Row Range("A80536").End(xlUp).Row
     
    For last_Row To 1 Step -1
        
    If Range("D" X).Value CDate("01/01/2011") Or Range("A" X).Value 50 Then '...'
            
    If dRng Is Nothing Then
                Set dRng 
    Range("D" X).EntireRow
            
    Else
                
    Set dRng Union(dRngRange("D" X).EntireRow)
            
    End If
        
    End If
     
    Next X
     
     
    If Not dRng Is Nothing Then dRng.Delete
      
    End Sub 
    Since this is for 5 sheets I didn't know how to adjust it better so I copied the formula 5 times and tried running it but I'm getting a error message "method union of object global failed" and it gets stuck at this line "**Set*dRng*=*Range("D"*&*X).EntireRow"

    Is there anyway to fix that?

    O yes and the code is super super fast
    ********
    ************thanks

  6. #6
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Help with macro to delete rows based on values

    PHP Code: 
    Sub GPE_COM()
     
    Dim X As Longlast_Row As Long
     Dim dRng 
    As RangeSh As WorkSheet       '**'
     
     
    For Each Sh In ThisWorkBook.WorkSheets   '**'
        
    If InStr("ARP@ARC&ARU&ARY@ARH."Sh.NameThen    '**'
    '      Sheets("ARP").Select   SHEET ARP'
           
    Sh.Select    '**'
           
    last_Row Range("A80536").End(xlUp).Row
           
    For last_Row To 1 Step -1
              
    If Range("D" X).Value CDate("01/01/2011") Or Range("A" X).Value 50 Then '...'
                  
    If dRng Is Nothing Then
                      Set dRng 
    Range("D" X).EntireRow
                  
    Else
                      
    Set dRng Union(dRngRange("D" X).EntireRow)
                  
    End If
              
    End If
           
    Next X 
           
    If Not dRng Is Nothing Then dRng.Delete
           Set dRng 
    Nothing    '**'
        
    End If
     
    Next Sh      '**'
    End Sub 
    Note: Where "ARC", "ARU" , "ARY" and "ARH" are Sheet Names
    Last edited by Sa DQ; 04-20-2012 at 12:14 AM.

  7. #7
    Forum Contributor
    Join Date
    11-25-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    107

    Re: Help with macro to delete rows based on values

    Quote Originally Posted by Sa DQ View Post
    PHP Code: 
    Sub GPE_COM()
     
    Dim X As Longlast_Row As Long
     Dim dRng 
    As RangeSh As WorkSheet       '**'
     
     
    For Each Sh In ThisWorkBook.WorkSheets   '**'
        
    If InStr("ARP@ARC&ARU&ARY@ARH."Sh.NameThen    '**'
    '      Sheets("ARP").Select   SHEET ARP'
           
    Sh.Select    '**'
           
    last_Row Range("A80536").End(xlUp).Row
           
    For last_Row To 1 Step -1
              
    If Range("D" X).Value CDate("01/01/2011") Or Range("A" X).Value 50 Then '...'
                  
    If dRng Is Nothing Then
                      Set dRng 
    Range("D" X).EntireRow
                  
    Else
                      
    Set dRng Union(dRngRange("D" X).EntireRow)
                  
    End If
              
    End If
           
    Next X 
           
    If Not dRng Is Nothing Then dRng.Delete
           Set dRng 
    Nothing    '**'
        
    End If
     
    Next Sh      '**'
    End Sub 
    Note: Where "ARC", "ARU" , "ARY" and "ARH" are Sheet Names

    Thank you again, but that's not exactly what I had in mind because with solving this problem new one is created.
    On the old example I was able to change column reference for each sheet.
    now code assumes that each sheet is exact same and that data is on every sheet in the same column, and that's not the case.

    for example if my ARH sheet has data that i'm looking for in columns E and N, i'm not able to adjust that with this code.

    Any suggestions for going around this problem?

  8. #8
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Help with macro to delete rows based on values

    Please list what columns (that contain the date and values) for each sheet name that the macro is to be run on.

  9. #9
    Forum Contributor
    Join Date
    11-25-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    107

    Re: Help with macro to delete rows based on values

    Quote Originally Posted by Trebor76 View Post
    Please list what columns (that contain the date and values) for each sheet name that the macro is to be run on.
    Sheets in the the woorkbook (ARP, APP, OSO, OPOS, POC)

    1. Dates
    All sheets column D except POC which is column I.

    2. Values (50 or 57)
    All sheets column A
    After finishing steps 1 and 2 I'm looking into going to steps 3 and 4


    3. Values (75FLG or 94VIL or values that start with 2)
    All column B except POC sheet which is column D.

    4. On sheet APP look at column J for amounts with (-) negative.


    The bottom line is to find all this values on all the sheets and delete the rows that contain those values.

    Thank you

  10. #10
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Help with macro to delete rows based on values

    Try this on a copy of your workbook in case the results are not as expected:

    Please Login or Register  to view this content.
    Note, any row in the range that is completly blank will also be deleted. This can be stopped if need be.

    Regards,

    Robert
    Last edited by Trebor76; 04-20-2012 at 07:56 PM.

  11. #11
    Forum Contributor
    Join Date
    11-25-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    107

    Re: Help with macro to delete rows based on values

    Quote Originally Posted by Trebor76 View Post
    Try this on a copy of your workbook in case the results are not as expected:

    Please Login or Register  to view this content.
    Note, any row in the range that is completly blank will also be deleted. This can be stopped if need be.

    Regards,

    Robert
    Thank. you Robert

  12. #12
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Help with macro to delete rows based on values

    You're welcome

+ 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