Showing posts with label VBA Code in Excel. Show all posts
Showing posts with label VBA Code in Excel. Show all posts

Monday, August 31, 2020

How to Assign a Macro

In this tutorial we are going to explain how you can Assign a Macro to the Command Button in the Microsoft Excel.

To assign a macro (one or more code lines) to the command button, execute the following steps:
  1. Right click CommandButton1 (make sure Design Mode is selected).
  2. Click View Code.

    View Code Image
    View Code
    The Visual Basic Editor appears.

  3. Place your cursor between Private Sub CommandButton1_Click() and End Sub.
  4. Add the code line shown below:
        Range("A1").Value = "Hello"
    
    Add Code Line Image
    Add Code Line
    Note: The window on the left with the name Sheet1 (Sheet1) and ThisWorkbook is called the Project Explorer. If the Project Explorer is not visible, click on View menu and then click on Project Explorer. If the Code window for Sheet1 is not visible, click Sheet1 (Sheet1). You can ignore the Option Explicit statement for now.

  5. Close the Visual Basic Editor.
  6. Click the command button on the sheet (make sure Design Mode is deselected).

Result:

Macro Result Image
Macro Result
Congratulations. You've just created a macro in Excel!



Wednesday, January 3, 2018

Overwrite an Existing File Using VBA Code in Excel

This tutorial will help you in, allowing your Macro/VBA code to overwrite an existing Excel file.

This will allows you to do things like:

Export a weekly report to a specific file and overwrite that file each week.

Note: The technique in this tutorial does not require any confirmation in order to overwrite the file.

Overwrite A File Using VBA

Add these two lines to any macro to allow them to overwrite a file without having the confirmation window appear: