Excel 2010 Bible

(National Geographic (Little) Kids) #1

Part VI: Programming Excel with VBA


854


End If
Next cell
End If
‘ Lower case
If OptionLower Then
For Each cell In Selection
If Not cell.HasFormula Then
cell.Value = StrConv(cell.Value, vbLowerCase)
End If
Next cell
End If
‘ Proper case
If OptionProper Then
For Each cell In Selection
If Not cell.HasFormula Then
cell.Value = StrConv(cell.Value, vbProperCase)
End If
Next cell
End If
Unload UserForm1
End Sub

The macro starts by turning off screen updating, which makes the macro run a bit faster. Next, the
code checks the type of the selection. If a range is not selected, the procedure ends. The remainder
of the procedure consists of three separate blocks. Only one block is executed, determined by
which OptionButton is selected. The selected OptionButton has a value of True. Finally, the
UserForm is unloaded (dismissed).

Testing the UserForm

To try out the UserForm from Excel, follow these steps:


  1. Activate Excel.

  2. Enter some text into some cells.

  3. Select the range with the text.

  4. Choose Developer ➪ Code ➪ Macros (or press Alt+F8).

  5. In the Macros dialog box, select ShowUserForm from the list of macros and then
    click OK. The UserForm appears.

  6. Make your choice and click OK.

Free download pdf