Excel VBA: Your first simple VBA code.

Ask a question

First Excel VBA Code

In this simple project we'll get to know how to:
  • create a simple button on an excel sheet.
  • create a simple code to change a certain cell value.
  • assign the command to the button we've created.

first of all we need to enable Developer Tab. in order to reach the tools we need while creating VBA based workbook project.

Creating the button

  • On the Developer tab, click Insert, select Button:

now just click and drag on your worksheet to create the button,you will be prompted by the following windows:
just click OK for now.

Time to Code the macro that will be executed when the button is pressed

  • on the developer tab click on Visual Basic.

your VB windows will be presented to you:
  • right click on your project select insert -> Module:

Button name:

switch back to Excel and right click on the button make sure the name does not contain any spaces since VB will NOT accept spaces in objects names.

go back to VB window.
  • write the following code in module1 window:

Sub myFirstCode()

    ActiveSheet.Cells(1, 1).Value = "Hello"

End Sub

Assign the macro to the button

close the VB window and right click on the button, just follow the pix below:

now you have assigned the macro to the button just click anywhere away from the button and then click on it and see the result.

Enjoy everybody !!