How to remove rows from list in excel by vba

m_vishal_c

Board Regular
Joined
Dec 7, 2016
Messages
209
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
HI i have two sheet in one workbook, " Sheet1" and " Need to be removed ". i need to remove all data from sheet1 from matching "Need to be removed " sheet column.

For example in Sheet1

NAMEADDRESS AGE COMPANYPOSITION
ABC1111
XYZ2121
ABC311311
DDD4141
GASDD4141
XYZ5151

<tbody>
</tbody>

in "Need to be removed" sheet
NAME
ABC
DDD
XYZ


<tbody>
</tbody>

I need to remove all rows from Sheet 1 where name=abc from "Need to be removed" list.

for that i am using below code but it does not remove all rows. Please guide me

Code:
Dim iListCount As IntegerDim iCtr As Integer




' Turn off screen updating to speed up macro.
Application.ScreenUpdating = False




' Get count of records to search through (list that will be deleted).
iListCount = Sheets("Sheet1").Range("A1:A5000").Rows.Count




' Loop through the "master" list.
For Each x In Sheets("Need to be removed").Range("A2:A30")
' Loop through all records in the second list.
For iCtr = 1 To iListCount
' Do comparison of next record.
' To specify a different column, change 1 to the column number.
If x.Value = Sheets("Sheet1").Cells(iCtr, 1).Value Then
' If match is true then delete row.
Sheets("Sheet1").Cells(iCtr, 1).[COLOR=#333333]EntireRow.Delete xlShiftUp[/COLOR]
' Increment counter to account for deleted row.
iCtr = iCtr + 1
End If
Next iCtr
Next
Application.ScreenUpdating = True
MsgBox "Done!"

Please guide me. much appreciate your support and time
thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Maybe this:
Code:
Option Explicit


Sub Remove()
    Dim s1 As Worksheet, s2 As Worksheet
    Dim i As Long, j As Long
    Dim lr1 As Long, lr2
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Need to be removed")
    lr1 = s1.Range("A" & Rows.Count).End(xlUp).Row
    lr2 = s2.Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    For i = 2 To lr2
        For j = lr1 To 2 Step -1
            If s1.Range("A" & j) = s2.Range("A" & i) Then
                s1.Range("A" & j).EntireRow.Delete
            End If
        Next j
    Next i
    Application.ScreenUpdating = True
    MsgBox "complete"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,748
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top