Macro to find and clear contents...

rcicconetti

New Member
Joined
Jan 16, 2016
Messages
34
I would like a macro to attach to a button and complete the following task.

If I enter a name in A3 and run the macro, it should search for the contents of A3 and clear all instances of that name on the sheet. Then clear A3 as well.

This will be on a scheduling assistant and the process will be used to eliminate an employee from the schedule.

Thank you
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Put this in a code module, and assign it to the button if you're using it directly on a spreadsheet, or a call to it if you are using a user form.

Code:
sub Remove_Employee

Dim rng_finder As Range
dim first, last as range

With Worksheets("MySheet").usedrange
    Set rng_finder = .Find(cells(3,1).text, LookIn:=xlValues, lookat:=xlWhole)
    If Not rng_finder Is Nothing Then
        Set first = rng_finder
        Do
            Set last = rng_finder
            last.value =""
            Set rng_finder = .FindNext(rng_finder)
        Loop While Not rng_finder Is Nothing And rng_finder.Address <> first.Address
    End If
End With
cells(3,1).value = ""

end sub
 
Upvote 0
I changed "MySheet" to ActiveSheet and the cell was actually A5, so I changed the cell to (5,1).
tHE BUGGER found a problem with ***last.value =""

Any suggestions?


Code:
sub Remove_Employee

Dim rng_finder As Range
dim first, last as range

With ActiveSheet.usedrange
    Set rng_finder = .Find(cells(5,1).text, LookIn:=xlValues, lookat:=xlWhole)
    If Not rng_finder Is Nothing Then
        Set first = rng_finder
        Do
            Set last = rng_finder
            last.value =""
            Set rng_finder = .FindNext(rng_finder)
        Loop While Not rng_finder Is Nothing And rng_finder.Address <> first.Address
    End If
End With
cells(5,1).value = ""

end sub
[/QUOTE]
 
Upvote 0
instead of last.value = ""

try Cells(last.row,last.column) = ""
 
Upvote 0
Hi there,
Sorry for the late response. I'm having connectivity problems abroad.
The bugger moved to the next line now, Run Time Error '91' "Object Variable or With block variable not set"
Loop While Not rng_finder Is Nothing And rng_finder.Address <> first.Address
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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