Share via

Excel VBA: Userform dynamic controls and class events

Anonymous
2023-08-12T12:05:56+00:00

Hello Everyone,

I have been using excel user forms since last 4/5 years for the data entry. Its working fine. Completely satisfied with it.

Now, i just wanted to be upgraded as i have seen the controls can be created on runtime and even the events can be passed through it.

Somehow, i managed to create the controls in user form frame by looping through columns from in first row. (Combo boxes, textboxes, labels). However, i want to add the class events referring to the controls. like dill combo boxes with conditions, text box events, addition / deletions based on the selected text boxes. I mean the dynamic text box controls which has values like start date, end date then the third box should display the days (end date - start date). also, if a text box has amount then the next text box should calculate tax value and subsequently the total value. I know this can be done through class events but somehow i am not able to start , perhaps array use for class events.

Any example file or any learning material or any reference book/online course with this would really help me to get through

Thank you in advance

Vikram

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2023-08-12T13:58:19+00:00

    Hi Vikram, I'm Femi and I'd be happy to help you with your question. It's great to hear that you're interested in upgrading your Excel user forms and exploring dynamic controls and class events. Working with dynamic controls and class events can indeed provide more flexibility and power to your applications. Here's a step-by-step guide and some resources that might help you achieve your goals:

    Step 1: Understand Class Modules and Events Class modules are a powerful feature in VBA that allow you to define custom objects with their own properties and events. By using class modules, you can create reusable control classes with associated events. In your case, you might want to create classes for various types of controls (like textboxes, combo boxes, etc.) and define events specific to them.

    Step 2: Creating Class Modules Let's say you want to create a class module for handling dynamic textboxes and their events. Here's a basic example: Insert a Class Module (Insert > Class Module) in the VBA Editor. Name it something like "clsTextboxEvent". In the class module, you can define properties and events. Here's an example of what your class module might look like: Public WithEvents txtBox As MSForms.TextBox

    Private Sub txtBox_Change() ' Your code to handle textbox change event End Sub

    Step 3: Create Dynamic Controls Now, in your userform code, you can create instances of this class dynamically: Dim TextBoxArray() As New clsTextboxEvent Dim TotalTextboxes As Long Dim i As Long

    Private Sub UserForm_Initialize() Dim c As Control

    ' Loop through controls in the frame (assuming your dynamic controls are in a frame) For Each c In Me.Frame1.Controls If TypeName(c) = "TextBox" Then TotalTextboxes = TotalTextboxes + 1 ReDim Preserve TextBoxArray(1 To TotalTextboxes) Set TextBoxArray(TotalTextboxes).txtBox = c End If Next c End Sub

    Step 4: Handle Events Now that you have the class instances with event handlers, you can customize the event handling code within the class module. For example, within the txtBox_Change event in the class module, you can implement logic to calculate days or tax based on the content of the textboxes.

    Resources: Books: "Excel VBA Programming For Dummies" by Michael Alexander and John Walkenbach, "Professional Excel Development: The Definitive Guide to Developing Applications Using Microsoft Excel, VBA, and .NET" by Rob Bovey, Dennis Wallentin, Stephen Bullen, and John Green.

    Online Courses: Websites like Udemy, Coursera, and LinkedIn Learning offer various Excel VBA courses that cover advanced topics.

    Websites and Forums: Websites like Stack Overflow and dedicated Excel VBA forums can be great places to ask specific questions and find examples.

    Microsoft Documentation: The official Microsoft documentation for VBA is a valuable resource to learn more about class modules and events: https://learn.microsoft.com/en-us/office/vba/api/overview/excel

    Hope this helps!

    Best Regards, Femi

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2023-08-14T13:25:51+00:00

    Glad to have been of help!

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2023-08-14T10:29:40+00:00

    Hi Vikram, I'm Femi and I'd be happy to help you with your question. It's great to hear that you're interested in upgrading your Excel user forms and exploring dynamic controls and class events. Working with dynamic controls and class events can indeed provide more flexibility and power to your applications. Here's a step-by-step guide and some resources that might help you achieve your goals: Step 1: Understand Class Modules and Events Class modules are a powerful feature in VBA that allow you to define custom objects with their own properties and events. By using class modules, you can create reusable control classes with associated events. In your case, you might want to create classes for various types of controls (like textboxes, combo boxes, etc.) and define events specific to them. Step 2: Creating Class Modules Let's say you want to create a class module for handling dynamic textboxes and their events. Here's a basic example: Insert a Class Module (Insert > Class Module) in the VBA Editor. Name it something like "clsTextboxEvent". In the class module, you can define properties and events. Here's an example of what your class module might look like: Public WithEvents txtBox As MSForms.TextBox Private Sub txtBox_Change() ' Your code to handle textbox change event End Sub Step 3: Create Dynamic Controls Now, in your userform code, you can create instances of this class dynamically: Dim TextBoxArray() As New clsTextboxEvent Dim TotalTextboxes As Long Dim i As Long Private Sub UserForm_Initialize() Dim c As Control ' Loop through controls in the frame (assuming your dynamic controls are in a frame) For Each c In Me.Frame1.Controls If TypeName(c) = "TextBox" Then TotalTextboxes = TotalTextboxes + 1 ReDim Preserve TextBoxArray(1 To TotalTextboxes) Set TextBoxArray(TotalTextboxes).txtBox = c End If Next c End Sub Step 4: Handle Events Now that you have the class instances with event handlers, you can customize the event handling code within the class module. For example, within the txtBox_Change event in the class module, you can implement logic to calculate days or tax based on the content of the textboxes. Resources: Books: "Excel VBA Programming For Dummies" by Michael Alexander and John Walkenbach, "Professional Excel Development: The Definitive Guide to Developing Applications Using Microsoft Excel, VBA, and .NET" by Rob Bovey, Dennis Wallentin, Stephen Bullen, and John Green. Online Courses: Websites like Udemy, Coursera, and LinkedIn Learning offer various Excel VBA courses that cover advanced topics. Websites and Forums: Websites like Stack Overflow and dedicated Excel VBA forums can be great places to ask specific questions and find examples. Microsoft Documentation: The official Microsoft documentation for VBA is a valuable resource to learn more about class modules and events: https://learn.microsoft.com/en-us/office/vba/api/overview/excel Hope this helps! Best Regards, Femi

    Hi Femi,

    Thank you very much!

    Yes, it would indeed help me to start with learning the class modules, their uses. I will start with some basic fields, say 2 /3 text boxes and perhaps comboboxes to get the basic idea with the step by step guide you provided.

    The ultimate aim is creating a generic/dynamic userform that refers the sheets based on the column heads. Not sure if this is possible but desperately looking for it.

    Thank you once again!

    With best regards,

    Vikram

    Was this answer helpful?

    0 comments No comments