Command button not working

LadyWraith

Board Regular
Joined
Aug 1, 2014
Messages
50
I inserted a command button into an existing excel spreadsheet. I went in vb editor for the button to write the code for the button's function. However, when you click the button, nothing happens. I think I forget/missed the line to make it do the macro. Does anyone see what I missed?

Option Explicit

Sub Button1_Click()

Dim i As Long
Dim sh As Object
Dim Yr, Yr_P, Yr_F, Q, Q_P, Q_F
Dim Qinput, TabName As String
Qinput = InputBox(Prompt:="Enter the year you want to create new slides for")
If Qinput = vbNullString Then
Exit Sub
Else
End If

Yr = Right(Left(Qinput, 4), 2)
Q = Right(Left(Cells(2, "F").Value, 2), 1)
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
What kind of button? If its an ActiveX button, then the Sub name must include the name of the button and the default naming convention for ActiveX button is CommandButton1, CommandButton2, etc.

If you inserted a form button, then you have to assign the macro to the button. Right-click on the button and choose Assign Macro
 
Upvote 0
Thanks. It would be an ActiveX button. I'll change the sub to CommandButton1. Do I still need the _Click , starl? Rightclicking the button does nothing, too. I did have it working yesterday, if only for a short time. I wonder what I did.
 
Last edited:
Upvote 0
You need to use the name assigned to the button. Honestly, the easiest way to set it up is, while in Design Mode, double-click on the button. That should automatically setup the Sub for you for the Click event
 
Upvote 0
You need to use the name assigned to the button. Honestly, the easiest way to set it up is, while in Design Mode, double-click on the button. That should automatically setup the Sub for you for the Click event

Oh dear. I tried double-clicking on the button...crashed my system. This is why I don't use a live file though. Plus, I saved the other code so I don't have to reinput it. Alright, from the beginning, insert the CommandButton1 in design mode, double-click the button and then I can go in and input the code. Name first, right?
 
Upvote 0
Draw the ActiveX button on your sheet. Double-click the button. Excel will automatically create the event with the correct name. It may not be CommandButton1.

or - draw button, with button selected, look at the Name field (where the cell address is usually). That's your button name. Go into the VBE, go to the SHEET module for that button's sheet. At the top of the VBE are two drop-downs. From the left drop-down, select the button. Excel will add a sub. If it's not the Click event (should be), then you can select the desired even from the right drop-down.
 
Upvote 0
Thank you for all of your help, starl. I got the button to work correctly. Part of it was those two fields/drop-downs not being enabled. Not sure about the freezing issue (took 3 system reboots before I got it to work. Why can I use a Mac or Oracle where I'm all comfy? :oops: Oh yeah. not at home) Now, I'm working on the other question I posted.

Btw, nice horse in your avatar. Arabian or Dutch Warmblood or Paso Fino?
 
Upvote 0
ActiveX controls don't work on Macs..

Galen is an Arabian/Quarter-Horse. That's him being the (Arabian) Region 6 Champion in 2012 :-) - Western Pleasure
 
Upvote 0

Forum statistics

Threads
1,223,155
Messages
6,170,405
Members
452,325
Latest member
BlahQz

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