In a previous post we have seen how to create a simple registration system in Excel using basic VBA macros. Such system consists of a registration form where users can enter the data (an onboarding form with the onboarding date, employee name, role, etc., in this example), and a registry that stores all the entries. In this post we see how to use form controls to create the registration form instead of using a range of cells. We will specifically be using ActiveX controls in Excel, which are programmable controls that allow to add functionality to the form.
Registration Form
The registration form consists of a group of ActiveX controls including labels, text boxes, combo boxes, and command buttons, added the worksheet (Sheet1). We can change the value or caption of the controls, or the position, font size, color, etc, in the properties window upon control right-click. The name of some of the controls has been updated for easier handling. For example, the combo boxes for day or month are now comboDay and comboMonth, instead of ComboBox1 and ComboBox2. The department field has also been put in a combo box (comboDept). The name of the text boxes has been changed to tbYear, tbName, tbRole, and tbManager. The command buttons are cbSend, cbClear, and cbReg.
The registration form needs to be initialized to populate the drop-down list of the combo boxes, and add any initial predefined values such as the current date for example. The best approach is to initialize the controls on file open through a Workbook_Open event procedure. The following line of code populates the list of months from the corresponding custom list available in Excel.
Sheet1.comboMonth.List = Application.GetCustomListContents(4)
We can choose to add the current date when opening the file. Once we know the current month and year, we can get the number of days in that month with the DateSerial function as indicated in the code below.
Send Form
In order to send the registration form, we need to program the click event of the corresponding command button. That’s shown per default when selecting the control in the sheet module. We can associate the controls to several other events depending on the type of control. Generally, most controls accept a change event, click, double-click, setting and losing focus events, key up, down, press, or mouse up, down, and move events.
The macro “SendForm” runs when clicking that command button and puts the registration data from the text and combo boxes into variables. We just need to make sure to refer to the worksheet where the controls were added (in this case is Sheet1).
Then it writes the data to the corresponding row in the other sheet (the registry). We need to get the last row with content (lr) in that sheet so that the entries are added to the first available row.
Clear Form
This macro just clears the contents in certain fields (we may want to leave the date though). The macro is called from a click event applied to the corresponding command button, and clears the value in the respective text and combo boxes in Sheet1.
Similarly, we can assign the click event of the last button (Go To Registry) to activate the registry worksheet (in this example that’s Sheet2).
Update Days
Additionally, we probably want to update the combo-box with the list of days whenever the month or year changes. We need to add the “Change” event to the month and year combo-boxes to trigger the macro below, which calculates the number of days for the selected month and year in the same way it’s done when initializing the form (explained earlier).
The ActiveX form controls added to the worksheet work in a similar way than Userform controls. In the next post we will see how to create the registration system through a Userform, which is probably the best approach from all three. Excel Userforms are very powerful and allow to further add functionality to the registration process. They are user friendly and ensure that the contents in the worksheet are not altered.
Click Here to download Excel Simple Registration with Form Controls.
No comments:
Post a Comment