+ Reply to Thread
Results 1 to 2 of 2

How to remove unwanted rows on multiple worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    07-02-2010
    Location
    Birmingham
    MS-Off Ver
    Excel 2007
    Posts
    21

    How to remove unwanted rows on multiple worksheets

    Hello all,

    I have a large spreadsheet with multiple worksheets.

    The first worksheet contains a list of study numbers (patients), and some corresponding data in columns. The list of study numbers is not consecutive - there are quite a few patients who have been removed.

    The subsequent worksheets have a consecutive list, and I want to remove the numbers that aren't in the first worksheet.

    I have attached a small dummy file which may make it clearer! There are 2 worksheets in the dummy file.
    Thanks,

    Adrian
    Attached Files Attached Files

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: How to remove unwanted rows on multiple worksheets

    Hi Adrian,

    Try this:

    Sub test()
    
    Dim wsEachSheet As Worksheet
    Dim wsMastSheet As Worksheet
    Dim lngLastRow As Long
    Dim lngLoopRow As Long
    Dim rngFindRange As Range
    
    Application.ScreenUpdating = False
    
    Set wsMastSheet = Sheets("List with exclusions removed")
    
    For Each wsEachSheet In ThisWorkbook.Worksheets
    
        If wsEachSheet.Name <> wsMastSheet.Name Then
        
            With wsEachSheet
        
                lngLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
            
                For lngLoopRow = lngLastRow To 2 Step -1
            
                    Set rngFindRange = wsMastSheet.Range("A2:A" & wsMastSheet.Cells(Rows.Count, 1) _
                        .End(xlUp).Row).Find(.Range("A" & lngLoopRow).Value, LookIn:=xlValues, lookat:=xlWhole)
        
                    If rngFindRange Is Nothing Then
                
                        .Rows(lngLoopRow).Delete
                    
                    End If
                
                Next lngLoopRow
            
            End With
            
        End If
        
    Next wsEachSheet
    
    Application.ScreenUpdating = True
    
    End Sub

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

+ 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