Visual Basic for Applications Fundamentals

download Visual Basic for Applications Fundamentals

of 5

Transcript of Visual Basic for Applications Fundamentals

  • 7/28/2019 Visual Basic for Applications Fundamentals

    1/5

    VBA for Excel fundamentals

    1. Visual Basic for Applications (VBA) is a dialect of Visual Basic available in all MicrosoftOffice applications as well as in other MS software (e.g., Visio) or non-MS software(e.g., AutoCAD, ArcINFO).

    2. To access the VBA IDE(Integrated Development Enviroment) find the Visual Basictoolbarby doing a right-click on any empty space of the Excel top tool bar and

    selecting the corresponding toolbar. Click on the first button in the VB toolbar to getyou to the VB IDE.

    3. The VBA IDEprovides a project navigation tree window (labeled Project - VBAProject) that lists all Excel/VBA projects currently open. This window may include anumber of password-protected files which are VBA projects loaded with Excel -- e.g.,Adobe Acrobat writer -- and which cannot be modified by the user. Your current

    project will be listed, by default, as VBAProject(Book1). If you save it with othername, the change will be reflected in the navigation tree window.

    4. Under your current project(i.e., your current Excel workbook), you will find a list ofworksheets (by default there will be three worksheets named Sheet1, Sheet2,Sheet3) followed by the workbook itself (default name, This Workbook). Also, abranch of the file tree may link to a Modules folder with at least one module (defaultname, Module1). If no Modules folder is available, and you want to add a module toyour project (workbook), use the Insert>Module option in the VBA IDE.

    5. A Macro is a VBA program that can be either recorded by the user or typed as VBcode.

    6. To record a macro use the option Tools>Macro>Record new macro... Any action youtake between pressing the OKbutton and stopping the recording will be sent to a

  • 7/28/2019 Visual Basic for Applications Fundamentals

    2/5

    VBA for Excel fundamentals

    VBA program within the first VB Module in your project. If no modules are availableat this point, Module1 will be added to the project.

    7. To see the macros available in your current project (workbook), use the optionTools>Macro>Macros. This will produce a form listing all macros (if any) available toyour workbook.

    8. To run a macro, after opening the form listing the available macros, click on themacro name and press the Run button.

    9. To edit a macro, after opening the form listing the available macros, click on the Editbutton. This will take you to the location of the macro within the VB IDE. Theobject to which the macro is attached (i.e., workbook, worksheet, form, or module)will be highlighted in the VB IDE's navigation tree window.

    10. The Excel control toolboxis useful for adding command buttons and other VBcontrols to your spreadsheet. Do a right click on any empty space on the Excel toptool bar and select the Control Toolbox.

    11. The Control Toolboxcontains a Design Mode toggle button, a Properties button,and a Code button, as well as a number of controls (check button, command button,

    list box, combo box, toggle button, spin button, scroll bar, label, image, andadditional controls).

  • 7/28/2019 Visual Basic for Applications Fundamentals

    3/5

    VBA for Excel fundamentals

    12. Command buttons can be used to start a program from a worksheet. Place acommand button by clicking on the command button icon in the Excel controltoolbox. Place and dimension the command button within the worksheet of interest.

    13. To modify the properties of the control button do a right-click on the button andselect the Properties option. Aproperties window appears where you can modify the

    button's properties such as the name, caption, font, etc.

    14. To add code to the control button double-click on the button. This will send youto the code area for the current worksheet in the VBA IDE. Type your code usingVisual Basic.

    15. If you want to assign a macro to a button, add the code Call Macro_name() to thebutton code.

    16. To get numerical data from the worksheet into your VBA code use functionsRange or Cells. For example, to get the value of cell "B2" into variable a in your VBAcode use:

    a = Range("B2").Value

    Alternatively, you could use

    a = Cells(2,2).Value

    to load your data value.

    17. To place data from your VBA code to the worksheetuse the same functionsRange and Cells, e.g.,

    Range("C2").Value = r1

    Cells(3,2).Value = r1

    18. To place string data from your VBA code to the worksheetuse, for example:Range("M2") = "Int. = "

    Cells(15,2) = "Int. = "

  • 7/28/2019 Visual Basic for Applications Fundamentals

    4/5

    VBA for Excel fundamentals

    19. Notice that function Range takes as argument a string referring to the cell ofinterest, e.g., Range("F15"), while the function Cells takes two arguments, namely,the indices for the row and column of the cell of interest, e.g., Cells(25,31).

    20. You can use functions MsgBoxand InputBoxfor output and input, respectively,into your VBA program.

    21. Excel provides a number offunctions that are not available in Visual Basic, e.g.,ACOS, ASIN, BESSELI, etc. You can use the cells in the worksheet to evaluateformulas that use these functions and then bring those values into your VBA code.

    22. To see a list of the functions available in Excel, click on a cell in a worksheet,place and equal sign in it and then click on the button labeled fxin the Excel toolbar.A listing of the functions by categories will be provided. The categories includeAll,Date & Time, Math & Trig, Statistical, etc. Click on any category to get a listing offunctions available.

    23. There is a category of functions referred to as Engineering functions that includeadvanced engineering mathematical functions, e.g., Besselfunctions, complexnumber conversion, error function, etc. If that category is not available in your Excelfunctions, use the option Tools>Add Ins... and mark the boxesAnalysis Toolpackand

    Analysis Toolpack - VBA to add the engineering functions. Press OKto load the

    functions.24. To label Excel cells with variable names, use the option Insert > Name > Define

    after selecting the cell (or cells) of interest. Type a name for the cell and that namewill serve as the cell identifier. This identifiers are only available for operationswithin Excel (e.g., formulas defined in cells). The identifier's names are not passed

    on to VBA code.25. Advantages and disadvantages of using Excel-VBA over Visual Basic:

    o The spreadsheet is the interface itself, you don't need to design and create aninput form -- although you can create one within Excel.

  • 7/28/2019 Visual Basic for Applications Fundamentals

    5/5

    VBA for Excel fundamentals

    o Many mathematical and non-mathematical functions are accessible throughthe spreadsheet (see items 22 and 23 above) which are not available in Visual

    Basic.o Excel provides graphics which are relatively easy to manipulate compared

    with graphics within Visual Basic.o Main disadvantage: cannot create stand-alone program with Excel/VBA. The

    user need to have Excelin his or her computer to be able to use yourExcel/VBA programs.