• No results found

Excel & Visual Basic for Applications (VBA)

N/A
N/A
Protected

Academic year: 2021

Share "Excel & Visual Basic for Applications (VBA)"

Copied!
16
0
0

Loading.... (view fulltext now)

Full text

(1)

1

Excel & Visual Basic for Applications (VBA)

‰ user interfaces

o on-sheet buttons

o toolbar buttons and custom toolbars o custom menus

o InputBoxand MsgBoxfunctions o userforms

2 On-Sheet Buttons

1) use the Forms toolbar to place a button on the worksheet 2) assign a macro to the button

3) format the button as desired

(2)

3 On-Sheet Buttons

draw button on worksheet

assign button to macro [ Note: assignment asked for

automatically in Excel 2002]

4 On-Sheet Buttons

change button text

(3)

5 On-Sheet Buttons

click button to run macro

use on-sheet buttons for macros dedicated to the workbook and worksheet, often to launch userforms

6 Toolbar Buttons

use toolbar buttons for tasks common to many workbooks, like editing tasks, e.g., center and wrap a title in the cell

(4)

7 Toolbar Buttons

click on "happy face" button

drag and place on formatting toolbar in a convenient location

change button image

pick from collection of standard buttons, or . . .

8 Toolbar Buttons

select Edit Button Image...

Button Editor

(5)

9

it is possible to add toolbar buttons when a certain workbook opens and remove them when the workbook closes, but this takes some extra work and is unusual

Toolbar Buttons

select Assign Macro...

macro typically from Personal.xls, in order to be general to all open workbooks

10 Toolbar Buttons

change button name to indicate function

close Customize window test button and macro

select headings &

click CW button result

widen column manually

(6)

11 Toolbar Buttons

toolbar buttons are mainly of use on your own computer system, not one of the lab systems

they are best used for special formatting macros that you want to apply to a variety of worksheets in different workbooks

toolbar buttons are sometimes used to run macros that are part of an Add-In →these are usually put on their own custom toolbar which is installed and "docked" with the other toolbars when the Add-In is activated

12 Custom Toolbars

Tools →Customize

Toolbars tab click New... button type in toolbar name:

(7)

13 Custom Toolbars

drag CW button to new toolbar more buttons could be added

a single toolbar is used to organize buttons that have a common theme, e.g., special formatting buttons or a dedicated engineering Add-In like calculations for flow in pipelines

once again, it is possible to display an entire toolbar only when a given workbook is opened and have it removed when the workbook is closed

14 Custom Menus

Tools →Customize

Commands tab New Menu category

click on New Menu

(8)

15 Custom Menus change menu name

Modify Selection

begin a menu group

drag commands to menu, both custom & built-in

drag to menu

completed menu

16 InputBox Function

simplified version

InputBox( “prompt”, “title”, default value ) optional

typical use

Xin = InputBox(“Enter value for x: “)

[ assign result of InputBox to variable for later use ] complete version

InputBox(“prompt”,”title”,default,xpos,ypos,helpfile,context) optional

(9)

17 InputBox Method (alternative to InputBox Function) object.InputBox(“prompt”,”title”, default, xpos, ypos, helpfile, context, type)

optional type codes 0 formula 1 number 2 string 4 logical 8 cell reference

16 error value, e.g., #N/A 64 array of values

codes can be combined to allow for inputs of multiple types, e.g., 3 for number or string

18 InputBox Method (alternative to InputBox Function)

(10)

19 MsgBox Function

MsgBox(“prompt”,buttons,”title”,helpfile,context) optional

text displayed in the message box

button codes vbOKOnly 0 vbExclamation 48 vbOKCancel 1 vbInformation 64 vbAbortRetryIgnore 2 vbDefaultButton1 0 vbYesNoCancel 3 vbDefaultButton2 256 vbYesNo 4 vbDefaultButton3 512 vbRetryCancel 5 vbDefaultButton4 768 vbCritical 16 vbSystemModal 4096 vbQuestion 32

MsgBox result is button clicked by user prompt items must be combined with &

MsgBox “The answer is “ & Xval & “ tons”

20 MsgBox Function

MsgBox Return Value

vbOK 1 vbCancel 2 vbAbort 3 vbRetry 4 vbIgnore 5 vbYes 6 vbNo 7

Putting line breaks into the message displayed

(11)

21 UserForms (custom dialog boxes)

the most flexible (and complicated) user interface feature Illustrate by example: a typical engineering formula

Calculate the Reynolds Number for liquid flow in a pipe

Du

Re=

ρ

µ

:

ρ

fluid density

D :

pipe inside diameter

u :

average fluid velocity

:

µ viscosity

Typical units and values

:

ρ

3 kg 900 m m 3 lb 55 ft

D :

55 mm 2.09 in

u :

2 m s ft 5 s : µ 0.005 Pa s3 cP 22 UserForms

Insert a UserForm in the Visual Basic Editor (VBE)

New and empty UserForm

(12)

23 UserForms Properties window lists properties of selected object, here the entire userform change name and title 24 UserForms

Add title and field for density

Properties window now refers to the label object change

caption Add textbox for value

(13)

25 UserForms

Add a “frame” to contain the units choices

remove frame caption

Add an option button in the frame for one units choice

“stretch” button so caption shows change title & caption

26 UserForms

change default button value to true

add button for alternate units with caption and default false

(14)

27 UserForms

add a command button to compute the answer

change command button name, caption & font

28 UserForms

add a Quit button with similar formatting

the UserForm is complete, but two important background elements are missing:

1) there is no way for the UserForm to appear for use 2) when the Compute and Quit buttons are clicked,

nothing will happen – there is no VBA code behind them

(15)

29 UserForms

double-click the Compute Re button

here, we must add code to carry out the calculations necessary and display the Reynolds Number result

convert units, as required compute the Reynolds Number and display the result

30 UserForms

insert a new module and add this Sub to start the UserForm

double-click the Quit button and add the code to remove (“unload”) the UserForm

Run the StartReynoldsNumber macro and enter values with units selected

(16)

31 UserForms

click the Compute Re button

click OK

then 1) you can set up and compute other cases

or 2) you click the Quit button to close out the UserForm

UserForms can be loaded automatically when the workbook is opened. This is called an “event handler.” And they can be unloaded automatically when the workbook is closed.

Although this is a good example of the use of UserForms, there are many other options and possibilities. You may need a good VBA reference book to take advantage fully of the potential of UserForms.

References

Related documents

customer need then solve those same morning of equations or bolster an optimization routine a nature of times using the same model, and word can transcend what happens when read

Select check add data using userform listbox and highlighting your spreadsheet b is add button on excel spreadsheet file: the userform that cell?. Either way holidays, add button

Integer used for most counting numbers range: from –32,768 to 32,767 uses 4 bytes of memory. uses 2 bytes

WG POSITIVE 12V TRUNK SIREN + PARKING LIGHTS RED WHITE/RED BROWN/BLUE 18A WG BLUE/WHITE 18A WG ORANGE/BLUE 18A WG ORANGE ORANGE TRUNK TRIGGER – CUT DOOR TRIGGER + + 12V DOOR TRIGGER

When you enroll in Medicare, you are automatically in the Original Medicare Plan unless you choose to join a Medicare Advantage plan (page 26).. Original Medicare is a

Now we want to write the macro for when a call comes they will note down all the above mentioned details into an excel sheet, and when they click on update that data should be

(Y screen will di al plan for co se the Back bu select the “ roll or Decl Elect or De Medical B IMPORT new An click mo NT INST umenos HS you must se You may on isplay your Cu

Excel workbook worksheets with cells chart sheets with graphs VBA Project modules with procedures and functions Option A Option B Option C OK Option A Option B Option C OK OK Option