Execute a macro from a command Button

Zsand

New Member
Joined
Nov 13, 2002
Messages
10
Execute a macro from a command Button

How do I create a command Button to run the code below with a button from my excel worksheet. This is the code. Can some one show me how to connect the code to the command Button?

Thanks


Option Explicit
'Dim MyArray(6, 4)
Dim MyArray(200, 4)
Public MyData As Range, c As Range

Private Sub TextBox1_Change()

End Sub

Private Sub ComboBox1_Change()

End Sub
Private Sub ComboBox2_Change()

End Sub
Private Sub ComboBox3_Change()

End Sub

Private Sub cmbAdd_Click()
'next empty cell in column C
Set c = Range("c65536").End(xlUp).Offset(1, 0)
Application.ScreenUpdating = False 'speed up, hide task
'write userform entries to database
c.Value = Me.Textbox1.Value
c.Offset(0, 1).Value = Me.TextBox2.Value
c.Offset(0, 2).Value = Me.TextBox3.Value
c.Offset(0, 3).Value = Me.TextBox4.Value
c.Offset(0, 4).Value = Me.TextBox5.Value
c.Offset(0, 5).Value = Me.TextBox6.Value
c.Offset(0, 6).Value = Me.ComboBox1.Value
c.Offset(0, 7).Value = Me.TextBox7.Value
c.Offset(0, 8).Value = Me.ComboBox2.Value
c.Offset(0, 9).Value = Me.ComboBox3.Value
c.Offset(0, 10).Value = Me.TextBox8.Value
c.Offset(0, 11).Value = Me.TextBox9.Value
c.Offset(0, 12).Value = Me.TextBox10.Value

'clear the form
With Me

.Textbox1.Value = vbNullString
.TextBox2.Value = vbNullString
.TextBox3.Value = vbNullString
.TextBox4.Value = vbNullString
.TextBox5.Value = vbNullString
.TextBox6.Value = vbNullString
.ComboBox1.Value = vbNullString
.TextBox7.Value = vbNullString
.ComboBox2.Value = vbNullString
.ComboBox3.Value = vbNullString
.TextBox8.Value = vbNullString
.TextBox9.Value = vbNullString
.TextBox10.Value = vbNullString


End With
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,454
Hello Zsand, welcome to the board.
If I'm right in thinking you want this command button to be on a userform
then here's how you can create it.
1) Go into the vb editor. (Press Alt+F11)
2) Make sure the Project Explorer is open. (Press Ctrl+R)
3) Locate your userform's name in the Project Explorer and double click it.
4) Make sure the toolbox is showing. (View > Toolbox)
5) Make sure your Properties window is showing. (Press F4)
6) From the toolbox click the CommandButton and draw it onto your userform.
7) In the Name property for the command button (will likely say
'CommandButton1' or some such) replace CommandButton1 with cmbAdd - Also, in
the Caption property, replace CommandButton1 with whatever text you want on
your button and press Enter.
8) Press Alt+Q to close the vb editor.

Is that what you're looking to do?
 

Forum statistics

Threads
1,136,607
Messages
5,676,778
Members
419,651
Latest member
alexanderguhr

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
Top