Never used a button before

M

max

I've inserted a command button and recorded (very simple):

Private Sub CommandButton2_Click()

Range("F9").Select
With Selection.Interior
.ColorIndex = 43
.Pattern = xlSolid
End With
End Sub

When I click the button, it takes me to the VBA screen. what am I doing
wrong?
 
J

Jim Thomlinson

So you grabbed a button off of the control toolbox and placed it on your
sheet. You recorded a macro and copied the code that was recorded into the
command button's click event. (you got the click event code stub by double
clicking the command button) I assume that my description is accurate so far?
When you added the button your sheet is in Design Mode. That mode allows you
to interact with the button without triggering the code to run. In order to
make the button operable you need to exit design mode. The first button on
the control toolbox looks like a triangle and ruler. Click that button to
exit design mode. Your button should now work correctly. You could tighten up
your code a bit as follows if you want...

Private Sub CommandButton2_Click()
'does not select the cell
With Range("F9").Interior
.ColorIndex = 43
.Pattern = xlSolid
End With
End Sub
 
T

Tom Ogilvy

what does the VBA screen say? (assume you mean it give you an error message).

Also, what version of excel are you using.
 
D

Dave Peterson

It sounds like you may be still in design mode.

If you're using xl2003 menus, there's a design mode icon on the Control toolbox
toolbar. Click that to toggle in and out of that state.

Private Sub CommandButton2_Click()
With me.range("F9")
.ColorIndex = 43
.Pattern = xlSolid
End With
End Sub

(dropping the selection.)
 
J

Jim Thomlinson

That is my best guess. Nice to have you back. We missed you. General
interest... Where did you get to. Somewhere good I hope.
 
J

Jim Thomlinson

You took a break from answering questions??? My sense of world order is
feeling a bit off knowing that. Death, taxes and Tom will answer the question
before you do. There are so few things you can count on in the world... <g>
 

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