VBA: Search for value and select and delete row with that value

Kristina96

New Member
Joined
Sep 30, 2019
Messages
33
Hello everyone,

I want to rearrange some data in an excel table.
I have a variable ID_Nr defined as double and it is a number between 2 and 100. I want the macro to look for that number in column A and if found select the whole row and delete it.
That way I am trying to eliminate all rows with a value that already appeared. It is supposed to keep only the first row with the entry "ID_Nr." in column A.

My tries lead to errors:
VBA Code:
Cells(1, 1).Select
    Cells.Find(What:="ID_Nr.", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate
    ActiveCells.Rows.Select
Selection.Delete

Thank you very much in advance for any kind of help!
Kristina
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try it this way

VBA Code:
Dim C as Range

Cells(1, 1).Select
Set C=Cells.Find(What:="ID_Nr.", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
C.EntireRow.Delete
 
Upvote 0
If it finds nothing then it will set C to nothing.

Get rid of the .activate which is what is causing the issue.

However the C.entirerow.delete, will also fail. so you need to add a test for C being nothing.

If not C is Nothing then C.EntireRow.Delete.
 
Upvote 0
If ID_Nr is a variable then it should not be in quotes, also it should not have a period (.) after it.
 
Upvote 0

Forum statistics

Threads
1,223,749
Messages
6,174,275
Members
452,553
Latest member
red83

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