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.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Can you please clarify as to how you'd like to split these labels and comboboxes into 3 different pages?
 
Upvote 0
Well that part, I am still figuring that out. The first part I would like to figure out is how to get these Labels and ComboBoxes to come up on page 1 of the MultiPage.

As a note, with the way that the first code I posted is written, it is actually O1 - T1 which have strings in them.

What I think I will do is, page 1 will look at O1 - Z1 to see if there are strings in them, if there is then I would like a label and combobox added. Every cell won't always be filled, and there won't be any blank cells between them.

Page 2 will look at O20 - T20 and Page 3 will look at O39 - T39
 
Upvote 0
This is perhaps a stupid question but why are you creating everything, including the form from scratch?

What code have you added for the comboboxes and the rest of the controls?

eg the comboboxes change event
 
Upvote 0
Norie asks a good question. Any reason why you're creating the userform and other controls at runtime? In any case, since it's somewhat unclear, here's a guess...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=darkblue]Dim[/color] TrackForm [color=darkblue]As[/color] VBComponent
    [color=darkblue]Dim[/color] tMP [color=darkblue]As[/color] MSForms.MultiPage
    [color=darkblue]Dim[/color] tLB [color=darkblue]As[/color] MSForms.Label
    [color=darkblue]Dim[/color] tCB [color=darkblue]As[/color] MSForms.ComboBox
    [color=darkblue]Dim[/color] wksNumb [color=darkblue]As[/color] Worksheet
    [color=darkblue]Dim[/color] TopVal [color=darkblue]As[/color] [color=darkblue]Integer[/color]
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] r [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] c [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]Dim[/color] Track [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    
    [color=darkblue]Set[/color] wksNumb = Worksheets("Numbers")
    
    TopVal = 10
    
    [color=darkblue]Set[/color] TrackForm = ActiveWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
    
    [color=darkblue]With[/color] TrackForm
        .Properties("Height") = 400
        .Properties("Width") = 350
        .Properties("Caption") = "Please select Track Codes"
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    [color=darkblue]Set[/color] tMP = TrackForm.Designer.Controls.Add("[color=darkblue]For[/color]ms.MultiPage.1")
    
    [color=darkblue]With[/color] 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"
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    [color=darkblue]With[/color] wksNumb
        r = 1
        [color=darkblue]For[/color] i = 0 [color=darkblue]To[/color] 2
            For c = 15 [color=darkblue]To[/color] 20
                Track = .Cells(r, c).Value
                [color=darkblue]If[/color] Track <> "" [color=darkblue]Then[/color]
                    [color=darkblue]Set[/color] tLB = tMP.Pages(i).Controls.Add("Forms.Label.1")
                    [color=darkblue]With[/color] tLB
                        .Name = "lab" & Track
                        .Caption = Track
                        .Left = 25
                        .Top = TopVal
                        .Height = 12
                        .Width = 150
                        .Font.Size = 10
                    [color=darkblue]End[/color] [color=darkblue]With[/color]
                    [color=darkblue]Set[/color] tCB = tMP.Pages(i).Controls.Add("Forms.ComboBox.1")
                    [color=darkblue]With[/color] tCB
                        .Name = "cb" & Track
                        .Left = 170
                        .Top = TopVal
                        .Height = 16
                        .Width = 150
                    [color=darkblue]End[/color] [color=darkblue]With[/color]
                    TopVal = TopVal + 16
                [color=darkblue]End[/color] [color=darkblue]If[/color]
            [color=darkblue]Next[/color] c
            TopVal = 10
            r = r + 19
        [color=darkblue]Next[/color] i
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    ShowTForm
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
The overall look of the userform doesn't really need to be created at run time. I can create a base Userform that has a MultiPage on it with 3 pages. What will have to input during run time is the number of labels and comboboxes. The values in the comboboxes will be input during run time as well.

I am trying to create a User interface that is as easy as possible for those that will end up using. This is the final piece of the puzzle that I have been working on for months now.
 
Upvote 0
Also I will be adding and Ok and Cancel buttons, those are the easy part, thus I did not add it in my initial post.
 
Upvote 0
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")
 
Upvote 0
How are you adding code for the events of the controls you are creating/adding?
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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