MultiPage Userform, Adding Labels using Macro help

nzepeda

Board Regular
Joined
Nov 11, 2010
Messages
58
I was able to write a macro that will build a Userform from scratch and add labels and combo boxes if there are values in certain cells, using the following:
Code:
Sub Testing()

Dim TrackForm As VBComponent

Dim tCB As MSForms.ComboBox
Dim tLB As MSForms.Label

Dim Numb As Worksheet
Set Numb = Worksheets("Numbers")

Dim Track As String

Dim oRow As Integer
Dim oCol As Integer

Dim Tval As Integer

oRow = 1
oCol = 15

Tval = 10

   
    ' Making Form
    Set TrackForm = ActiveWorkbook.VBProject _
    .VBComponents.Add(vbext_ct_MSForm)
    With TrackForm
        .Properties("Height") = 400
        .Properties("Width") = 350
        On Error Resume Next
        .Properties("Caption") = "Please select Track Codes"
    End With
             
    For oCol = 15 To 69
        Track = Numb.Cells(oRow, oCol).Value

        If Track <> "" Then
            Set tLB = TrackForm.Designer.Controls.Add("Forms.Label.1")
            With tLB
                .Name = "tName"
                .Caption = Track
                .Left = 25
                .Top = Tval
                .Height = 12
                .Width = 150
                .Font.Size = 10
            End With

            Set tCB = TrackForm.Designer.Controls.Add("Forms.ComboBox.1")
            With tCB
                .Name = "tCode"
                .Left = 170
                .Top = Tval
                .Height = 16
                .Width = 150
            End With

        Tval = Tval + 16
        End If

    Next oCol
       
    ShowTForm
End Sub

Cells A1 through F1 have strings in them, thus 6 labels and comboboxes are added.

What I would like to do is add a MultiPage, and split these labels and comboboxes into 3 different pages.

I was able to figure out how to create a MultiPage with 3 pages onto a UserForm through a macro with this.

Code:
Sub Testing()

Dim TrackForm As VBComponent

Dim tMP As MSForms.MultiPage

    ' Making Form
    Set TrackForm = ActiveWorkbook.VBProject _
    .VBComponents.Add(vbext_ct_MSForm)
    With TrackForm
        .Properties("Height") = 400
        .Properties("Width") = 350
        On Error Resume Next
        .Properties("Caption") = "Please select Track Codes"
    End With
        
    Set tMP = TrackForm.Designer.Controls.Add("Forms.MultiPage.1")
        With tMP
            .Name = "HostMP"
            .Width = 325
            .Height = 200
            .Left = 10
            .Top = 70
            .Pages(0).Name = "Socal"
            .Pages(0).Caption = "Socal Host"
            .Pages(1).Name = "Losal"
            .Pages(1).Caption = "Los Alamitos Host"
            .Pages.Add.Name = "CalX"
            .Pages(2).Caption = "Cal Expo Host"
        End With
       
    ShowTForm
End Sub

What I can't figure out is how to combine these 2 Macros into 1.

I want it to make the UserForm, then add in a MultiPage and then in the Multipage add Labels and Comboboxes.

Any help would be much appreciated.
 
Domenic
Thank you very much.

As soon as I changed how I was setting tLB and tCB it worked great.

Original
Code:
Set tLB = tMP.Pages(0).Controls.Add("Forms.Label.1")
Set tCB = tMP.Pages(0).Controls.Add("Forms.ComboBox.1")

Now
Code:
Set tLB = tMP.Pages(0).Controls.Add("Forms.Label.1")
Set tCB = tMP.Pages(0).Controls.Add("Forms.ComboBox.1")

You're very welcome! Thanks for the feedback!

By the way, your original and new code seem to be the same. :-)
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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