+ Reply to Thread
Results 1 to 7 of 7

Delete Duplicate rows vb needs to run on all worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    04-09-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    25

    Delete Duplicate rows vb needs to run on all worksheets

    Hi i have the below code which runs on deleting duplicate code which i found in an excel manual. I am having some trouble as i want to convert the code so that it will run on all worksheets i have which are numbered such as 1,2,3,4 etc.

    This is so it makes the workbook easy maintenance for when deleting or adding worksheets which happens quite regularly.

    Thanks



    Sub Delete_Duplicate_Rows()
    '
    Dim str1 As String, str2 As String
    Dim r1 As Long, r2 As Long, c As Long
    '
    Application.ScreenUpdating = False
    With Sheets("1").UsedRange
    
    '
    For r1 = .Rows.Count To 2 Step -1
    str1 = ""
    For c = 1 To .Columns.Count
    str1 = str1 & Cells(r1, c).Value
    Next c
    For r2 = r1 - 1 To 1 Step -1
    str2 = ""
    For c = 1 To .Columns.Count
    str2 = str2 & Cells(r2, c).Value
    Next c
    If str1 = str2 Then
    Rows(r1).Delete
    Exit For
    End If
    Next r2
    Next r1
    '
    End With
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by sandy.beach; 04-22-2009 at 02:03 PM. Reason: did not label as code

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Delete Duplicate rows vb needs to run on all worksheets

    You need to to edit that original post, highlight all that code, then click the CODE icon (it looks like this: #).

    It's a forum rule and makes the code easier to read on the forum.
    =========

    Meanwhile, you want this macro to run on ALL sheets, or just sheets you've labeled 1,2,3,4...etc.?

    For ALL sheets, give this a try, just wrapped your code in another loop for all the worksheets (untested without your workbook).
    Option Explicit
    
    Sub Delete_Duplicate_Rows()
    Dim str1 As String, str2 As String
    Dim r1 As Long, r2 As Long, c As Long
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
    
    For Each ws In Worksheets
    
        With ws.UsedRange
        
            For r1 = .Rows.Count To 2 Step -1
                str1 = ""
                For c = 1 To .Columns.Count
                    str1 = str1 & Cells(r1, c).Value
                Next c
                    
                For r2 = r1 - 1 To 1 Step -1
                    str2 = ""
                    For c = 1 To .Columns.Count
                        str2 = str2 & Cells(r2, c).Value
                    Next c
            
                    If str1 = str2 Then
                        Rows(r1).Delete
                    Exit For
                    End If
                Next r2
            Next r1
        
        End With
    
    Next ws
    
    Application.ScreenUpdating = True
    End Sub
    Last edited by JBeaucaire; 04-20-2009 at 08:23 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    04-09-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Delete Duplicate rows vb needs to run on all worksheets

    thanks for that but i was after the script just working on all 1,2,3,4 etc pages.

    What i plan to do is create a button which i can click and it will run this script which will run on all pages labelled 1,2,3,4 etc.

    Just means when adding/removing pages its less admin work.

    Thanks

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Delete Duplicate rows vb needs to run on all worksheets

    Hello sandy.beach,

    Which sheet names do you want to exclude?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    04-09-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Delete Duplicate rows vb needs to run on all worksheets

    I wanted to exclude sheets named:

    Menu
    and
    Main
    I just can't figure out how to do it.

    Thanks

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Delete Duplicate rows vb needs to run on all worksheets

    Hello sandy.beach,

    The macro will loop through all the sheets in the workbook. I used the Select Case statement to exclude the worksheets you posted. Here is the updated macro code.
    Sub Delete_Duplicate_Rows()
    '
    Dim str1 As String, str2 As String
    Dim r1 As Long, r2 As Long, c As Long
    Dim Wks As Worksheet
    '
    Application.ScreenUpdating = False
    
    For Each Wks In Worksheets
    Select Case Wks.Name
    Case "Main", "Menu"
      'Do nothing Skip these sheets
    Case Else
    With Wks.UsedRange
    
    '
    For r1 = .Rows.Count To 2 Step -1
    str1 = ""
    For c = 1 To .Columns.Count
    str1 = str1 & Cells(r1, c).Value
    Next c
    For r2 = r1 - 1 To 1 Step -1
    str2 = ""
    For c = 1 To .Columns.Count
    str2 = str2 & Cells(r2, c).Value
    Next c
    If str1 = str2 Then
    Rows(r1).Delete
    Exit For
    End If
    Next r2
    Next r1
    '
    End With
    Next Wks
    End Select
    
    Application.ScreenUpdating = True
    
    End Sub

  7. #7
    Registered User
    Join Date
    04-09-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Delete Duplicate rows vb needs to run on all worksheets

    THanks for your help everyone !!!!

    I got it working with your help

+ 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