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.
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
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.
• 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 →