Dynamically Added SpinButtons

Multiverse

New Member
Joined
Nov 15, 2019
Messages
16
Hi

I've been working on a UserForm which loads in data from a Worksheet range, and dynamically creates a varying number of TextBoxes. I now want to dynamically add a SpinButton to each line I'm displaying in the form. The SpinButton will increment or or reduce the value of a line by a penny (or a cent). How should I handle the SpinButton _Change() Sub when the number of buttons will be dynamic?

This is how I've created the buttons:

VBA Code:
    For i = 3 To PostingsLastRow
        n = n + 1
        Set cSpinButton = FormInvoice.FrameInvLines.Controls.Add("Forms.SpinButton.1")
        With cSpinButton
            .Name = "SpinButton" & n
            .Orientation = fmOrientationHorizontal
            .Height = 15
            .Width = 50
            .Left = 940
            .Top = p
        End With
        p = p + 20
    Next i

I've uploaded an image so you can see when I'm going with this.

Many thanks!
 

Attachments

  • demo.png
    demo.png
    6.8 KB · Views: 13
@Domenec Thanks ever so much. That code works perfectly.

You're very welcome, glad I could help.

Now that the spinButtons are working, the dynamic textboxes won't update as I want them to. When I debug.print their values, they have changed, but not on the form. Updating static textboxes works fine. Does this mean I have to create a class for my dynamic textboxes as well?

If you want to trigger an event procedure for your dynamically created textboxes, then yes, you'll need to create another class.
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,214,834
Messages
6,121,873
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top