Attached a Macro to Button

Zsand

New Member
Joined
Nov 13, 2002
Messages
10
Hi,

I attempted to attach the following Macro to a button. But when I tried to run it the following errors:

Complie Error Invalid inside procedure.

I need your help.

Thanks

Sub commandButton()

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

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

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Zsand

Just put Option Explicit at the top of the module, not inside the sub.
Code:
Option Explicit

Sub commandButton()

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

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
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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