Lab 2_fall2011

Lab 2_fall2011 - Lab 2 Introduction to Visual Basic...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
23 Lab 2 Introduction to Visual Basic Programming EXCEL Visual Basic Tutorial Problems (Version May 27 th , 2009) Note: Your textbook follows a slightly different approach for inputting a VB code. The method shown is here is relatively simple and straight forward. The problem below shows how to create a VB button and rename it to a name you like. But you don’t have to rename it; you can just leave it as the default name of “Sub CommandButton1_Click()” and the program should still work. Test Problem-1 : Develop a simple calculator that will add or subtract numbers two numbers entered in a spreadsheet. The final result will look like this: The VB code for the ADD and SUBTRACT buttons are: Option Explicit Option Base 1 Private Sub codeAdd_Click() Dim a As Single, b As Single, c As Single a = Cells(1, 2) b = Cells(3, 2) c = a + b Cells(5, 2) = c
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
24 End Sub Private Sub codeSubtract_Click() Dim a As Single, b As Single, c As Single a = Cells(1, 2) b = Cells(3, 2) c = a - b Cells(5, 2) = c End Sub We will use EXCEL cells to input the values and to return the values right back to one of the cells. The command “Cells(row,col)” allows us to access cell values. Here we placed the command buttons ADD and SUBTRACT directly on the spreadsheet. This can be done by clicking on the “Insert” option under the Developer tab and then clicking on the command button option under ActiveX Control. Later click within the spreadsheet to create the button. Right click on the button and select “Properties” (or directly click on “Properties” next to the Design Mode button on top of the EXCEL ribbon) and set the properties name to “codeAdd” and set the caption to “ADD” (You can also Or you can right click on the button, go down to “Command Button Object”, then go over and click “Edit.” This will allow you to change the caption right on the button itself). Double click on the newly created command button and input the subroutine given above. Note, when you double clicked-- the system automatically created a subroutine
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/14/2011 for the course CE 3010 taught by Professor Clement during the Fall '09 term at Auburn University.

Page1 / 5

Lab 2_fall2011 - Lab 2 Introduction to Visual Basic...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online