Hi Tom

In "Sheet 1" there is a list of students in column A

In Sheets 2, 3, 4, 5, 6, 7, 8, 9, 10, there is also a list of students in column A

Is it possible to write a piece of VBA that will delete any row in Sheets: 2, 3, 4, 5, 6, 7, 8, 9 and 10 that does not contain a student in "Sheet 1"

in other words if "Sheet 1" contains the names


in cells "A2", "A3" and "A4"

and Sheet 2 contains the names


in cells "A2", "A3", "A4", "A5", "A6" and "A7"

when the VBA is run "Sheet 2" will contain the names


in cells "A2", "A3" and "A4$

Row "A" will contain column headings and must remain unaltered by the code.

Once again, if this request takes you beyond the reasonable amount of time one might expect to spend on a single request, please let me know.  In the first instance, I need to know if what I request is possible.

Thanks in advance



This should be placed in a general module (in the VBE, with your workbook/project selected in the project explorer, do Insert=>Module. Paste the code there)

Sub abc()
Dim sh As Worksheet, sh1 As Worksheet
Dim r As Range, lastrow As Long, i As Long, j As Long
Set sh = Worksheets("Sheet1")
Set r = sh.Range("A2", sh.Cells(sh.Rows.Count, "A").End(xlUp))
For i = 2 To 10
 Set sh1 = Worksheets("Sheet" & i)
 lastrow = sh1.Cells(sh1.Rows.Count, "A").End(xlUp).Row
 For j = lastrow To 2 Step -1
   If Application.CountIf(r, sh1.Cells(j, "A").Value) = 0 Then
   End If
 Next j
Next i
End Sub

The code worked in my test as I expected.  However, you should test it on a copy of your workbook until you are satisfied it behaves as you wish/expected.

Note that you have a space in your sheet names.  ex:  "Sheet 2".  My code is expecting a sheet name like  "Sheet2".  If you do in fact have a space in your names, then change this line

Set sh = Worksheets("Sheet1")


Set sh = Worksheets("Sheet 1")

and this line

Set sh1 = Worksheets("Sheet" & i)


Set sh1 = Worksheets("Sheet " & i)

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.