Delete Macro

triegaardtm

New Member
Joined
Nov 29, 2012
Messages
30
HI


I need help creating a Macro to delete a row.

Private Sub btnRemoveCrate()

Dim CrateRef As String
CrateRef = cbCrateRef.Text

**the Macro must find the selected text in the first column of active sheet and the delete the whole row containing that text.

Rows(???Containing the CrateRef???).Select
Selection.Delete Shift: = xlUP

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi

Try:

Code:
Private Sub btnRemoveCrate()

Dim rFound As Range

Set rFound = Activesheet.Column("A").Find(What:=cbCrateRef.Text,LookAt:=xlPart,MatchCase:=False)
If Not rFound Is Nothing Then rFound.EntireRow.Delete
End Sub
 
Upvote 0
Sorry, that should have read "Columns":

Set rFound = Activesheet.Columns("A").Find(What:=cbCrateRef.Text,LookAt:=xlPart,MatchCase:=False)
 
Upvote 0
thanx firefly2012, its working.

Can you possibly explain why rFound is nothing? and then why it deletes the column. have no idea how your code works.
 
Upvote 0
The Not preceding the "rFound is Nothing" reads in English "assuming rFound is not nothing {ie it is something as value was found}".

So thatr check is required to ensure that the sought value actually was found in the column.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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