ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Assign button to clear all, "except" (https://www.excelbanter.com/excel-programming/353303-assign-button-clear-all-except.html)

darkwood[_2_]

Assign button to clear all, "except"
 

I want to create a button that, when pressed will clear all rows that
have "Y" in column E. How do I accomplish this? I know nothing about
macros, so please keep that in mind when explaining. I really
appreciate the help!

-darkwood


--
darkwood
------------------------------------------------------------------------
darkwood's Profile: http://www.excelforum.com/member.php...o&userid=29948
View this thread: http://www.excelforum.com/showthread...hreadid=512410


Jim Thomlinson[_5_]

Assign button to clear all, "except"
 
Here is some code that should do what you want. You will need to change the
line that says Sheet2 to the sheet you want to clean. The code assumes that
your Y's and N's are in Cells E2 and below... You can change that if you
need...

Sub ClearData()
Dim rngToDelete As Range
Dim rngToSearch As Range
Dim strFirstAddress As String
Dim rngFound As Range
Dim wks As Worksheet

Set wks = Worksheets("Sheet1")
With wks
Set rngToSearch = .Range(.Range("E2"), _
.Cells(Rows.Count, "E").End(xlUp))
End With

Set rngFound = rngToSearch.Find(What:="Y", _
LookAt:=xlWhole, _
MatchCase:=False)
If rngFound Is Nothing Then
MsgBox "Nothing to Delete"
Else
Set rngToDelete = rngFound
strFirstAddress = rngFound.Address
Do
Set rngToDelete = Union(rngFound, rngToDelete)
Set rngFound = rngToSearch.FindNext(rngFound)
Loop Until rngFound.Address = strFirstAddress
rngToDelete.EntireRow.Delete
End If
End Sub

You did not mention how you wanted to call the procedure so I can not tell
you the best place to put it...
--
HTH...

Jim Thomlinson


"darkwood" wrote:


I want to create a button that, when pressed will clear all rows that
have "Y" in column E. How do I accomplish this? I know nothing about
macros, so please keep that in mind when explaining. I really
appreciate the help!

-darkwood


--
darkwood
------------------------------------------------------------------------
darkwood's Profile: http://www.excelforum.com/member.php...o&userid=29948
View this thread: http://www.excelforum.com/showthread...hreadid=512410




All times are GMT +1. The time now is 07:03 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com