Thursday, March 17, 2022

Assign a macro to a form or a control button

You can use a Form control button or a command button (an ActiveX control) to run a macro that performs an action when a user clicks it. Both these buttons are also known as a push button, which can be set up to automate the printing of a worksheet, filtering data, or calculating numbers. In general, a Form control button and an ActiveX control command button are similar in appearance and function. However, they do have a few differences, which are explained in the following sections.

Button (Form control)

Example of a Forms toolbar button control

Command button (ActiveX control)

Example of an ActiveX command button control

In the sections below, learn how to add a macro to a button in Excel—for Windows or the Mac.

Note: ActiveX controls are not supported on the Mac.

Macros and VBA tools can be found on the Developer tab, which is hidden by default.

The first step is to enable it. For more information, see the article: Show the Developer tab.

Developer tab on the ribbon

Add a button (Form control)

  1. On the Developer tab, in the Controls group, click Insert, and then under Form Controls, click Button Button image .

  2. Click the worksheet location where you want the upper-left corner of the button to appear. The Assign Macro popup window appears.

  3. Assign a macro to the button, and then click OK.

  4. To specify the control properties of the button, right-click the button, and then click Format Control.

Add a command button (ActiveX control)

  1. On the Developer tab, in the Controls group, click Insert, and then under ActiveX Controls, click Command Button Button image .

  2. Click the worksheet location at which you want the upper-left corner of the command button to appear.

  3. In the Controls group, click View Code. This launches the Visual Basic Editor. Ensure that Click is chose in the drop-down list on the right. The sub procedure CommandButton1_Click (see the figure below) runs these two macros when the button is clicked: SelectC15 and HelloMessage.

    A subprocedure in the Visual Basic Editor

  4. In the subprocedure for the command button, do either of the following:

    • Enter the name of an existing macro in the workbook. You can find macros by clicking Macros in the Code group. You can run multiple macros from a button by entering the macro names on separate lines inside the subprocedure.

    • As necessary, add your own VBA code.

  5. Close the Visual Basic Editor, and click Design Mode Button image to ensure design mode is off.

  6. To run the VBA code that is now part of the button, click the ActiveX command button that you just created.

  7. To edit the ActiveX control, make sure that you are in design mode. On the Developer tab, in the Controls group, turn on Design Mode.

  8. To specify the control properties of the command button, on the Developer tab, in the Controls group, click Properties Button image . You can also right-click the command button, and then click Properties.

    Note: Before you click Properties, make sure that the object for which you want to examine or change properties is already selected.


    The Properties box appears. For detailed information about each property, select the property, and then press F1 to display a Visual Basic Help topic. You can also type the property name in the Visual Basic Help Search box. The following table summarizes the properties that are available.

If you want to specify

Use this property

General:

Whether the control is loaded when the workbook is opened. (Ignored for ActiveX controls.)

AutoLoad (Excel)

Whether the control can receive focus and respond to user-generated events.

Enabled (Form)

Whether the control can be edited.

Locked (Form)

The name of the control.

Name (Form)

The way the control is attached to the cells below it (free floating, move but do not size, or move and size).

Placement (Excel)

Whether the control can be printed.

PrintObject (Excel)

Whether the control is visible or hidden.

Visible (Form)

Text:

Font attributes (bold, italic, size, strikethrough, underline, and weight).

Bold, Italic, Size, StrikeThrough, Underline, Weight (Form)

Descriptive text on the control that identifies or describes it.

Caption (Form)

Whether the contents of the control automatically wrap at the end of a line.

WordWrap (Form)

Size and Position:

Whether the size of the control automatically adjusts to display all the contents.

AutoSize (Form)

The height or width in points.

Height, Width (Form)

The distance between the control and the left or top edge of the worksheet.

Left, Top (Form)

Formatting:

The background color.

BackColor (Form)

The background style (transparent or opaque).

BackStyle (Form)

The foreground color.

ForeColor (Form)

Whether the control has a shadow.

Shadow (Excel)

Image:

The bitmap to display in the control.

Picture (Form)

The location of the picture relative to its caption (left, top, right, and so on).

PicturePosition (Form)

Keyboard and Mouse:

The shortcut key for the control.

Accelerator (Form)

A custom mouse icon.

MouseIcon (Form)

The type of pointer that is displayed when the user positions the mouse over a particular object (standard, arrow, I-beam, and so on).

MousePointer (Form)

Whether the control takes the focus when clicked.

TakeFocusOnClick (Form)


Macros and VBA tools can be found on the Developer tab, which is hidden by default, so the first step is to enable it

  1. Go to Excel > Preferences… > Ribbon & Toolbar.

  2. In the Customize the Ribbon category, in the Main Tabs list, select the Developer check box, and then click Save.

    Select the checkbox next to Developer to add it to the ribbon

Add a button (Form control)

Follow these steps:

  1. On the Developer tab, click Button Button image .

  2. Click the worksheet location where you want the upper-left corner of the button to appear. The Assign Macro popup window appears.

    Note: If you have already inserted a button, you can right-click on it, and select Assign Macro.

  3. Assign a macro to the button and click OK.

  4. To specify the control properties of the button, right-click it, and then select Format Control....
     

Add a command button (Visual Basic control)

  1. On the Developer tab, click Button Button image .

  2. Click the worksheet location where you want the upper-left corner of the command button to appear.

    Note: If you have already inserted a button, you can right-click on it, and select Assign Macro.

  3. In the Assign Macro dialog box, select New, which will open the Visual Basic Editor (VBE) to a pane with the following code:

        Sub ButtonX_Click()       End Sub
  4. In the subprocedure for the command button, between the Sub and End Sub lines, do either of the following:

    • Enter the name of an existing macro in the workbook. You can run multiple macros from a button by typing the macro names on separate lines inside the sub procedure

    • Add your own VBA code.

  5. Click Design Mode Button image to ensure that design mode is off, then close the Visual Basic Editor window.

  6. To edit the button, right-click it and choose Visual Basic.

  7. To specify the control properties of the button, right-click it, and then select Format Control....

Need more help?

You can always ask an expert in the Excel Tech Community or get support in the Answers community.

See Also

5 comments:

  1. Special thanks to HACK VANISH, a professional hacker I’ve been hiring for the past 2 years because, I find him to be an outstanding hacker who has aided me in various hacking related issues, ranging from fixing my poor FICO credit score of about 437 TransUnion, 462 Equifax to 802 plus excellent score, tracking my cheating husband’s phone and currently has helped me recover my lost crypto funds from an online scam investment I recently ventured into, I must confess he is the best, quite brilliant, ever since I discovered him through a positive review I saw on Wallet Hub and reached out to him via Email: Hackvanish @ Gmail. com, and Phone: +1(747) 293-8514, to which he responded in less than 10mins, I have never regretted working with him, he’s been quite helpful and highly efficient. I Would recommend his services to anyone without hesitation.

    ReplyDelete
  2. PINNACLE CREDIT SPECIALIST remains the best credit expertise I have known. I had 23 negative things on my credit that were holding me back. One of them is from Capital one another from sears which I paid off and I am not sure of the others but they are both 6 years old. I filed bankruptcy 5years ago and settled with all of these. My attention was called that they are still on my reports I recently got referred by my best friend to hire (pinnaclecreditspecialist@gmail.com / +1 585 466 4373) which I did and he helped me fix my credit and my score improved by a huge leap. I recommend anyone that needs to fix his or her credit to get through with PINNACLE CREDIT SPECIALIST. He is just the best. Kudos to the entire team PINNACLE!

    ReplyDelete
  3. I got an email from Experian saying my score went up, so I decided to check my scores on Credit Karma and my scores has dropped 56 points from 600 to 544. I went to check what happened and there’s no changes listed. Now I just feel defeated. I just don’t want to bother anymore. I’ve been keeping my credit use under 30%, paying my card off every month, been setting all my bills up for auto pay. I’ve been trying so hard and it’s all for nothing. Thank to PINNACLE CREDIT SPECIALIST who came through for me. I’ve been working so hard with them to rebuild my credit, and seeing that score goes up again, that made me so proud. Within 5 days of working with PINNACLE they increased my score to TU 819, EX 810, EQ 816. All negative items have been removed; credit card debt marked as paid on time. I sincerely recommend PINNACLE CREDIT SPECIALIST to anyone out their who has similar issues with me. You can get them on: pinnaclecreditspecialist@gmail.com OR call them on +1 (585) 466 4373. Let them know Morton Howard, I referred you.

    ReplyDelete
  4. In today's financially-driven world, credit plays a vital role in shaping our financial futures. A good credit score can open doors to opportunities such as securing loans, obtaining favourable interest rates, and even influencing job prospects. However, individuals often find themselves facing credit issues due to various reasons. This has led to the emergence of credit repair companies and specialists who claim to help individuals regain control over their credit scores. I’ll analyse the effectiveness of credit repair companies and specialists, emphasizing Pinnacle Credit Specialist as an organization that truly delivers results.
    1. Understanding Credit Repair:
    Credit repair involves the process of improving an individual's creditworthiness and credit score by identifying and rectifying inaccuracies, errors, and negative items on their credit reports. Credit repair companies and specialists possess the expertise and knowledge required to navigate complex credit systems and guide individuals towards credit improvement.
    2. The Effectiveness of Credit Repair Companies:
    While scepticism may surround the credit repair industry, there are ample success stories that testify to the effectiveness of credit repair. Reputable companies like PINNACLE CREDIT SPECIALIST have consistently helped countless individuals recover from financial setbacks, improve their credit scores, and restore their financial stability. Through their proven methodologies and personalized strategies, PINNACLE CREDIT SPECIALIST can make a significant difference in an individual's credit journey.
    3. Expertise and Specialization at PINNACLE CREDIT SPECIALIST:
    PINNACLE CREDIT SPECIALIST is an exemplary credit repair company that stands out in the industry due to its commitment to excellence. With years of experience and a team of skilled professionals, they possess deep knowledge of credit reporting laws and regulations. This expertise allows them to challenge questionable items on credit reports, negotiate with creditors, and remove inaccuracies that may be holding individuals back from achieving a good credit score.
    4. Tailored Solutions for Every Situation:
    Each individual's credit situation is unique, and PINNACLE CREDIT SPECIALIST recognizes this by providing personalized solutions. Through careful analysis of credit reports, they create customized plans of action designed to address specific credit issues. Whether it's removing late payments, erroneous collections, or other negative marks, PINNACLE CREDIT SPECIALIST offers a comprehensive approach that gives individuals the best chance at credit restoration.
    5. Trust and Positive Testimonials:
    A credit repair company's reputation is crucial in determining its credibility. PINNACLE CREDIT SPECIALIST has earned trust through its stellar track record of success stories from satisfied clients. Numerous testimonials highlight their ability to improve credit scores significantly, often in a shorter time frame than individuals could achieve on their own. Such positive feedback strengthens the argument that PINNACLE CREDIT SPECIALIST are indeed capable of fixing credit and are worth considering.
    PINNACLE CREDIT SPECIALIST have proven their ability to fix credit by delivering concrete results. While there may be unscrupulous entities within the industry, it is essential to recognize and recommend companies like PINNACLE CREDIT SPECIALIST that genuinely make a positive impact on the lives of individuals struggling with credit issues. Their expertise, tailored solutions, and strong reputation demonstrate the effectiveness of credit repair companies in empowering individuals to reclaim their financial well-being. Therefore, for those seeking credit improvement, considering PINNACLE CREDIT SPECIALIST as a partner is a wise decision.

    CONTACT THEM BY EMAIL: PINNACLECREDITSPECIALIST@GMAIL.COM

    ReplyDelete
  5. I can invest my last dollar with only one person and that’s Mike Defi who prove to be the most honest and trustworthy Expert trader. him the best account manager I have ever known or heard of. him has been helping me. I started with just $1000 and now am getting great profits like $11,670 upward. You get to withdraw yourself after 6-7days of trading, no extra commissions.
    If you are really interested, You can contact him via:
    Email: Defimike93@gmail.com

    ReplyDelete