18.11.2014 Views

Microsoft Office

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating UserForms<br />

You can’t use Excel very long without being exposed to dialog boxes. Excel,<br />

like most Windows programs, uses dialog boxes to obtain information,<br />

clarify commands, and display messages. If you develop VBA macros, you<br />

can create your own dialog boxes that work very much like those that are built<br />

into Excel. These dialog boxes are known as UserForms.<br />

Why Create UserForms?<br />

Some macros that you create behave exactly the same every time that you execute<br />

them. For example, you may develop a macro that enters a list of your sales<br />

regions into a worksheet range. This macro always produces the same result and<br />

requires no additional user input. You may develop other macros, however, that<br />

perform differently under different circumstances or that offer options for the<br />

user. In such cases, the macro may benefit from a custom dialog box.<br />

The following is an example of a simple macro that makes each cell in the<br />

selected range uppercase (but it skips cells that have a formula). The procedure<br />

uses VBA’s built-in StrConv function.<br />

Sub ChangeCase()<br />

For Each cell In Selection<br />

If Not cell.HasFormula Then<br />

cell.Value = StrConv(cell.Value, vbUpperCase)<br />

End If<br />

Next cell<br />

End Sub<br />

This macro is useful, but it can be improved. For example, the macro would be<br />

more helpful if it could also change the cells to lowercase or proper case (only the<br />

first letter of each word is uppercase). This modification is not difficult to make,<br />

but if you make this change to the macro, you need some method of asking the<br />

IN THIS CHAPTER<br />

Why create UserForms<br />

UserForm alternatives<br />

Creating UserForms: An<br />

overview<br />

UserForm examples<br />

More on creating UserForms<br />

715

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!