Plots In Excel Userform

I have an Excel VBA userform that loads with some data from a spreadsheet when it opens/initialises. I am also wanting to include in the userform a chart image (the chart exists on the spreadsheet). Dec 19, 2014 That can not be the actual code you are using as it is incomplete. Once corrected it worked for me. Userform with Image and commandbutton. Option Explicit Dim ChartPu As Chart Private Sub CommandButton1Click UpdateChart End Sub Private Sub UpdateChart Dim Fname As String Set ChartPu = Sheets('Sheet2').ChartObjects('MyChart').Chart With ChartPu.Parent.Width = 300.Parent.Height = 150 End.

  1. Advanced Excel Userform Examples
  2. Vba Excel Userform Example
You can easily add multiple controls to a UserForm in Excel, simply by dragging and dropping the controls from the toolbox on to the form. Do you know you can create the UserForm controls at runtime? Yes, you can and here I am sharing an example that shows how to create controls dynamically in a UserForm using VBA and save all the data to an Ms-Access table.

The example here does not cover or create all the ActiveX Controls that are available in the toolbox, but it creates some of the most important controls (like the textbox, combo, checkbox etc.) dynamically and adds it to a Frame inside the UserForm.

Related Post:Excel VBA – Loop Through All Textboxes in UserForm and Clear the Values

It’s a simple data entry form, where a user can enter details about an Employee (its like an Employee master) and save the data in a database (Ms-Access).

The most important part of this example is that it creates an ActiveX Command Button with Events, at run time, so you can save the data.

Create a Table in Ms-Access

Since, I’ll be saving the data in the UserForm in an Ms-Access table, I’ll first create the table. Open Ms-Access, and from the top menu, find Create and choose Table option. Name the table as Employee and add few fields to the table. See the image.

Add a UserForm in Excel

Open an Excel file and save the file as Macro or in .xlsm format. To add a UserForm, you will have to open the VBA editor. You can open the editor by right clicking the Sheet1 tab (or any tab at the bottom of your worksheet) and choose View Code option or simply press Alt+F11 keys.

Next, open Project Explorer window, right click the project to Insert a UserForm.

From the ToolBox, drag and drop a Frame on to the UserForm. This is the only control we’ll actually add at design time. The Frame will serve as the main or parent container for all the controls that we’ll create at run time. Don’t add any caption to the frame.

Add a Module to Create ActiveX Data Objects

The UserForm in this example, has a ComboBox (a dropdown list) , which has a list of Countries. Since this list will have many values, I don’t want to hardcode the values. Instead, I’ll add the list of countries in Sheet2 in my worksheet (I have named Sheet2 as Country), and extract and fill this data to the ComboBox, at run time.

I am using ADO connection object to set a connection and fetch records from Sheet2 (for the combo box). Therefore, you must add the ADO library reference in your application.

From the top menu in your VBA editor, click Tools and choose References. In the Reference window, find and select Microsoft ActiveX Data Objects 6.1 Library (or any other version).

Now, add this code in your Module.

The Code to create and add UserForm Controls dynamically

The code looks a bit lengthy, but its useful. I’ll explain it.
I have declared all the control objects in the beginning. The frame named childFrame is attached to the main frame or the parent frame (which we have added in the beginning). The childFrame will actually have all the other controls that I am creating at run time.

Also Read:How to Create a Cascading Combo Box in Excel using VBA

I have set the width and height of the childFrame, so it can accomodate all the controls.

Next, I'll add the remaining controls, like the textboxes, labels, option buttons, checkboxes etc. to the childFrame. Each control is placed on the frame using its .Top and .Left properties. This might look like time consuming, but its worth it. Even, dragging and dropping the controls at a specific location on the form, takes time.

I also have a Submit button. I have created a CommanButton object in the beginning using the WithEvents property. Buttons, like any other controls, have events. I need to use the button’s Click event to submit the data.

Creating and using the events at run time is simple.

Here's how you declare the Event.

Along with this, I have two more procedures.
Procedure fillCombo() fills a ComboBox with a list of Countries. See how I am using an SQL like query SELECT *from [Country$] to fetch the data from my Excel worksheet. The procedure takes a parameter in the form of an object.

Procedure SaveData() saves the data in the UserForm to an Ms-Access table named Employee (I have created in the beginning).

The procedure takes few string parameters. These are the data fetched from the dynamically created controls in the UserForm.

Here again I am creating an ADO connection. Look at the connection string and the Provider I have used to connect to an Ms-Access database.

That’s it. Run the application. If everything goes right, you should have a simple, basic form, with some dynamically created controls. Fill the form with some values and click the Submit button.

Thanks for reading.

← PreviousNext →


Plots in excel userform excel
Like this Article? Subscribe now, and get all the latest articles and tips, right in your inbox.

Advanced Excel Userform Examples

The question of drawing lines and shapes on a userform comes up quite often.
The normal approach for straight horizontal or vertical lines is to use the border of a label control and set its height or width to 1.
When it comes to anything more fancy then the approach is to create the drawing on the worksheet using the built-in auto-shapes and then at design time manual copy and paste it to an image control's picture property.
Or for the brave you could go down the API route.
I have taken the auto-shape route. The main differences being that shapes can be added at run time and multiple shapes can be handled.
Shapes can be created and formatted as required before they are painted to the userform.
The example above is a very gaudy example of what is possible.
Here is the syntax used to create the blue box
As you can see it is very similar to normal VBA code used when adding and formatting auto-shapes.
Plots in excel userform formulas
I have included two versions of the same demonstration for down loading.
One version contains all the modules and class code within a project. It makes seeing the demonstration easier but requires extra work sharing the code if used on other projects.
Download the Self contained demonstration
The other version of the demonstration requires the UFDraw.xla to be loaded in order for it to work.
If you are using Excel 2000 then the code will automatically try and locate and load the file. Failing that it will give you the choice to locate the file manually.
If you are using versions xl2002 or xl2003 then you will need to adjust your security setting to allow the code to automatically set vbproject references.
This message box will be displayed if you have not enabled 'Trust access to Visual Basic Projects'
The is the dialog needed to change the setting.
If you load the xla manually then you can run the Demo macro rather than the DemoUserformDrawing macro, which is linked to the button on the worksheet.

Vba Excel Userform Example

Download the XLA demonstration