help creating a button

R

Raz

Sub Button1_Click()
Range("A1:D10").Clear
Range("A12:D15").Clear
End Sub

I am using a button with this code above to reset those cells to blank
(deleting prevoius data). But it is deleting all the formatting also.
What do I need to change or add to the code above to clear the text and
numbers keeping all the formatting as it is.

Also should I use a Form control button or ActiveX control button ?
 
D

Dave Peterson

..clearcontents

And the choice of buttons is up to you. I like to use the controls from the
Forms toolbar. I find them easier to work with.

And if I have lots of them in the worksheet/workbook, I think they behave better
than the controls from the control toolbox toolbar.
 
R

Rick Rothstein

As others have mentioned, ClearContents is the method you need to use. I
thought you might be interested in knowing that you can combine those two
statements into a single statement...

Sub Button1_Click()
Range("A1:D10,A12:D15").ClearContents
End Sub

Note that each disjointed range reference is separated by a comma and that
the entire reference is enclosed within the same set of parentheses.
 
R

Raz

thanks

Rick Rothstein said:
As others have mentioned, ClearContents is the method you need to use. I
thought you might be interested in knowing that you can combine those two
statements into a single statement...

Sub Button1_Click()
Range("A1:D10,A12:D15").ClearContents
End Sub

Note that each disjointed range reference is separated by a comma and that
the entire reference is enclosed within the same set of parentheses.
 
R

Rick Rothstein

You are welcome. I just want to note one thing... I made a misstatement in
my original post... when I said "...and that the entire reference is
enclosed within the same set of parentheses" (which is true, but also
somewhat obvious), what I meant to say was "...and that the entire reference
is enclosed within the same set of quote marks" (that is, do NOT put each
disjointed range within its own quote marks).
 

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

Top