Tuesday, September 22, 2015

Button

In the previous section, we learned how to write codes with VBA Editor. But to active the macro on the spread sheet, we must click Macro on the developer tool bar, which is un-useful. Now we learn how to run a macro using a button on a spread sheet.

At first, we register a macro with the VBA editor. Open up the VBA editor from the developer tool bar by clicking Visual Basic on the developer tool bar.

the images cited from home and learn.

On the coding window, we write as follows:

Sub TestSub

Range(ActiveCell, "D6").Select

End Sub

We save this macro. Then we will try to use this macro from a button which we will create now. Note that ActiveCell represents the cell on which your cursor currently is. Needless to say, Range represents a range. Range(ActiveCell, "D6") represents a range "Active cell" to D6. If you write as Range("A1:D6") or Range("A1","D6"), it represents a range A1 to D6.

After the dot, a verb Select is written. This is a method which enables us to select certain cells. Thus, if we run this code, this macro selects the range of cells Active cell to D6.

Now we have written codes and made a macro. Now we will make a button on the spread sheet. Click the Excel icon to go back to the spread sheet.
the image cited from home and learn.

 Go see the developer bar of the spread sheet. On the Control panel of the bar, you try to find Insert item. Click Insert, then click the rectangular thing on the list.
the image cited from home and learn.

Then move your mouse to the spread sheet. Pressing the left mouse button, you can draw a rectangular which will be a button which we will use for the macro.

As you let your go of the left mouse button, a dialogue box appears. This dialogue box is used to assign a macro to a button. Assign the macro TestSub which we've created just now to the button.
Now you have a button which says "Button 1"on the spread sheet.

Then right-click on the button and we will see a list of some items. Click Edit Text from the list.
the image cited from home and learn.

You can change the name now. Change the name "Button 1" to "TestSub" to make it easier to see what the button is.
the image made by myself

Now we have the button which runs the macro we've created. Clicking the button, you will see the range Active cell to D6 gets selected.

References

Home and Learn, accessed 22nd September 2015.