Yes, check out VBA's MsgBox function and its InputBox function. Alternatively, you might want to use Excel's InputBox method.
There is no easy way to do this because each CommandButton has its own Click event procedure. One solution is to call another procedure from each of the CommandButton_Click procedures. Another solution is to use a class module to create a new class. This technique is described in Chapter 15.
There is no direct way to do this. One solution is to write a macro that saves the chart to a GIF file and then loads the GIF file into an Image control on the UserForm.
Removing the Close button on a UserForm's title bar requires some complex API functions. A simpler approach is to intercept all attempts to close the UserForm by using a UserForm_QueryClose event procedure in the code module for the UserForm. The following example does not allow the user to close the form by clicking the Close button:
Private Sub UserForm_QueryClose _ (Cancel As Integer, CloseMode As Integer) If CloseMode = vbFormControlMenu Then MsgBox "You can't close the form like that." Cancel = True End If End Sub
In some cases, using links to worksheet cells can slow your application because the worksheet is recalculated every time a control changes the cell . In addition, if your UserForm has a Cancel button, the cells might have already been changed when the user clicks Cancel.
You can't create a control array, but you can create an array of Control objects. The following code creates an array consisting of all CommandButton controls:
Private Sub UserForm_Initialize() Dim Buttons() As CommandButton Cnt = 0 For Each Ctl In UserForm1.Controls If TypeName(Ctl) = "CommandButton" Then Cnt = Cnt + 1 ReDim Preserve Buttons(1 To Cnt) Set Buttons(Cnt) = Ctl End If Next Ctl End Sub
Yes, the Hide method keeps the UserForm in memory but makes it invisible. The Unload statement unloads the UserForm, beginning the "termination" process (invoking the Terminate event for the UserForm) and removing the UserForm from memory.
By default, each UserForm is modal, which means that it must be dismissed before you can do anything else. However, you can make a UserForm modeless by using vbModeless as the argument for the Show method. Here's an example:
UserForm1.Show vbModeless
Test for the version of Excel that the user is running and then execute a separate procedure if the version is Excel 2000 or later. The following code demonstrates how:
Sub ShowUserForm() If Val(Application.Version) >= 9 Then ShowModelessForm Else UserForm1.Show End If End Sub Sub ShowModelessForm() Dim frm As Object Set frm = UserForm1 frm.Show 0 ' vbModeless End Sub
Because the ShowModelessForm procedure is not executed in Excel 97, it will not cause a compile error.
You can do this with a UserForm. Chapter 15 describes several different techniques, including one in which the code gradually stretches a shape inside a frame while the lengthy macro is running.
You can't use the shapes directly with a UserForm, but you can do so indirectly. Start by adding a shape to a worksheet. Then select the shape and choose Home Clipboard Copy. Activate your UserForm and insert an Image object. Press F4 to display the Properties window. Select the Picture property and press Ctrl+V to paste the Clipboard contents to the Image control. You might also need to set the AutoSize property to True .
There's no need to do that. Use VBA's GetOpenFilename method. This method displays an Open dialog box in which the user can select a drive, directory, and file.
You can use a monospaced font such as Courier New for the ListBox. A better approach, however, is to set up your ListBox to use two columns . (See Chapter 14 for details.)
Yes, you can use an array. The statement that follows adds three items to ListBox1 .
ListBox1.List = Array("Jan", "Feb", "Mar")
Many of Excel's dialog boxes can be displayed by using the Application.Dialogs method. For example, the following instruction displays the dialog box that enables you to format numbers in cells:
Application.Dialogs(xlDialogFormatNumber).Show
Use the Object Browser to display a list of the constants for the built-in dialog boxes. Press F2 from the VBE, select the Excel library, and search for xlDialog. You'll probably need to use some trial and error to locate the constant that corresponds to the dialog box that you want to display.
In Excel 2007, you can execute Ribbon commands (including those that display a dialog box) by using the ExecuteMso method along with the control name . The statement that follows, for example, displays the dialog box that enables you to format numbers in a cell:
Application.CommandBars.ExecuteMso("NumberFormatsDialog")
See Chapter 22 for more information.
The Dialogs method (and the ExecuteMso method) will fail if the context isn't appropriate. For example, if you attempt to display the Chart Type dialog box ( xlDialogChartType ) when a chart is not activated, you'll get an error message.
Yes. Set up a UserForm with the controls that you use most often. Then choose File Export File to save the UserForm. When you want to add a new form to another project, choose File Import File.
Yes, but it requires some complex API functions.
Controls added to a UserForm do nothing unless you write event handler procedures for them. These procedures must be located in the code module for the UserForm, and they must have the correct name.
You can, but it's probably not worth the effort. You can write code to determine the video resolution and then use the Zoom property of a UserForm to change its size. The normal way to deal with this matter is simply to design your UserForm for the lowest resolution that will be used - probably a 1024 — 768 display.
Yes. Use the RefEdit control for this. See Chapter 14 for an example.
Yes, you can set the UserForm's Left and Top properties. But for these to be effective, you need to set the UserForm's StartUpPosition property to .
Yes. See Chapter 15 for an example.