A few words of thanks would be greatly appreciated.

Excel VBA: Your first simple VBA code.

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 !!

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « Excel VBA: Your first simple VBA code. », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).