Reset button to clear cells

  • Thread starter Thread starter M.Siler
  • Start date Start date
M

M.Siler

I have a worksheet that have only certain fields the user can enter data
into. It would be nice to have a button at the top of the sheet that would
clear all the user fields. I can't find anything in Excel to do this so I'm
assuming that it's a VBA thing. I'm not so go at Excel VBA so I don't know
how to do this. Also, the fields are single cells, ranges and all over the
sheet.
 
If you can do it manually - ie paint all the cells then remove contents AND
you always do it this way you could record a macro which would convert it to
VBA.

Cheers

John
 
Hi
one easy way: Record a macro while doing this manually and assign this
macro to a button
 
Good idea,

I've got the macro record now, but how to I put a button in the sheet?
 
I guess the better question is... now that I have the macro recorded and a
button on my sheet how to I get the button to fire the macro?
 
I'd give that range of all those cells a nice name.

Select the first cell and ctrl-click to keep adding cells to the selection.

then type MyCellsToClear in the namebox (to the left of the formula bar)
and hit enter!!!

then have your macro include something like:

worksheets("sheet1").range("mycellstoclear").clearcontents


then show the Forms toolbar (view|toolbars|forms)

On that toolbar, there's a button icon. Click on that and then draw a box on
the sheet (topleftcorner to bottomrightcorner) where you want the button.

When you let go, you'll be prompted for the name of the macro to assign.

(You can always rightclick on the button and select "assign macro" later if you
want.)

You may want to give the user a little prompt to verify:

Option Explicit
Sub ClearThemAll()

Dim resp As Long

resp = MsgBox(Prompt:="Are you sure", Buttons:=vbOKCancel)

If resp = vbCancel Then Exit Sub

Worksheets("sheet1").Range("mycellstoclear").ClearContents

End Sub
 
No good... I did the following:

To draw a button on the worksheet, you will click the button icon in the
toolbar. Click in the worksheet and drag to draw any size button. When you
release the mouse, the "Assign Macro" dialog will appear. You can now select
which macro should run when you click the button.

I got a button, but I never got the "Assign Macro" dialog.

Here what is shown: =EMBED("Forms.CommandButton.1","")

I right click. I select Properties. I try everything and I never see "Assign
Macro" What gives?
 
If you are using the command button from the Controls toolbar,
you will not have an "Assign Macro" option. Instead, you'll have
a "View Code" option. Select that and put your code in the event
procedure created by View Code.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Thanks... That worked nicely!

Dave Peterson said:
I'd give that range of all those cells a nice name.

Select the first cell and ctrl-click to keep adding cells to the selection.

then type MyCellsToClear in the namebox (to the left of the formula bar)
and hit enter!!!

then have your macro include something like:

worksheets("sheet1").range("mycellstoclear").clearcontents


then show the Forms toolbar (view|toolbars|forms)

On that toolbar, there's a button icon. Click on that and then draw a box on
the sheet (topleftcorner to bottomrightcorner) where you want the button.

When you let go, you'll be prompted for the name of the macro to assign.

(You can always rightclick on the button and select "assign macro" later if you
want.)

You may want to give the user a little prompt to verify:

Option Explicit
Sub ClearThemAll()

Dim resp As Long

resp = MsgBox(Prompt:="Are you sure", Buttons:=vbOKCancel)

If resp = vbCancel Then Exit Sub

Worksheets("sheet1").Range("mycellstoclear").ClearContents

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top