Dynamically added userform controls don't work

Dan777

Board Regular
Joined
Jan 4, 2012
Messages
56
I've a problem/s:
1) The command buttons dont work
2/3) How do I repaint cells and remove code that I add

the form displays great, the command buttons dont work
Code:
Private Sub UserForm_Initialize()
Dim TextLocation As Integer, j As Integer, dH As Integer, LeftPos As Integer
Dim x As Integer, i As Integer, TopPos As Long, lineCount As Integer
Dim NumberOfStreams As Long, NumChem As Long, MaxCharLenLabel As Long
Dim StreamUNIT As String
Dim TotalTextbox As String
Dim NewTextBox As Object
Dim NewLabelBox As Object
Dim NewComboBox As Object
Dim VBCodeMod As Object
Dim ChemLabel() As Variant
   
Dim wsG1 As Worksheet
Dim wsStIN As Worksheet
    
    Set wsG1 = ThisWorkbook.Sheets("Global Input1")
    Set wsStIN = ThisWorkbook.Sheets("Input Waste Stream")
'   Add the OptionButtons
    TopPos = 4  'initalize the top position
    dH = 17  'the TopPos change in height
    
    With wsG1
        StreamUNIT = .Cells(18, 2).Value  'obtains the dimesions for the stream inputs (desired output dimension)
        NumChem = wsStIN.Range("A" & Rows.Count).End(xlUp).Row - 19 'chemical row begins in row 20
        .Cells(10, 3).Value = NumChem  'write number of chemicals to global 1
    End With  'With wsG1
'ask for the number of streams
    NumberOfStreams = HowMany("Streams")
    ReDim ChemLabel(1 To NumChem) As Variant  'reduce memory used
    MaxCharLenLabel = 0  'initialize maximum character length for temp-form labels
    
    With wsStIN
        For i = 1 To NumChem  'set up labels for temporary form, the chemical of intrest (per line of code)
            ChemLabel(i) = .Cells(19 + i, 1).Value 'First Chemical is in Row 20
            If (MaxCharLenLabel < Len(ChemLabel(i))) Then MaxCharLenLabel = Len(ChemLabel(i)) 'Finds the Max Character Length
        Next i
    End With  'With wsStIN
    
'LabelBox 1
    With Label1
    'Input the Title information of the new Form
        .Caption = "The Dimensions of this stream are in : [% - " & StreamUNIT & "]"
        .Height = 11
        .Width = 200
        .Left = 8
        .Top = TopPos
        .BackColor = RGB(255, 255, 255)
        .AutoSize = False
        TopPos = TopPos
    End With  'With NewLabelBox
'chemical labels: the number of chemicals  the 1 additional is for the units; the first label
'LabelBox 2-to-(NumberOfStreams+1)
    For i = 1 To NumChem
'        Set NewLabelBox = MassPercForm.Controls.Add("forms.label.1")
        Set NewLabelBox = MassPercForm.Controls.Add("Forms.Label.1")
        TopPos = TopPos + dH  'reset TopPosition   .Controls.Add("Forms.Label.1", "Test" & labelCounter, True)
        With NewLabelBox
            .TextAlign = fmTextAlignRight
            .Width = MaxCharLenLabel * 4 + 2
            .Caption = ChemLabel(i)
            .Height = 15
            .Left = 8
            .Top = TopPos + 2.2 'Add 2.2 to align text with TextBox
            .AutoSize = False
        End With  'With NewLabelBox
    LeftPos = MaxCharLenLabel * 4 + 15  'position of textbox (user input information)
'add textbox's for input of info : Side-by-Side with Label box
'TextBox 1-to-(NumberOfStreams + 0)  :  for INPUT
        Set NewTextBox = MassPercForm.Controls.Add("forms.textbox.1")
        With NewTextBox
            .TextAlign = fmTextAlignCenter
            .Width = 50
            .Height = 15
            .Left = LeftPos  'position of textbox (user input information)
            .Top = TopPos
            .AutoSize = False
        End With  'With NewTextBox
    Next i  'For i = 1 To NumberOfStreams
    
    LeftPos = LeftPos + 50 + 10 '50 is the width of the individual textboxs
    If LeftPos < 150 Then LeftPos = 150   'Assign LeftPosition for secondary textboxes
'   INPUT  stream total  %mass  --this is the question box  for the TOTAL MASS
'LabelBox (NumberOfStreams + 2) :  just a label
        Set NewLabelBox = MassPercForm.Controls.Add("forms.label.1")
        With NewLabelBox
            .Caption = "What is the Total MASS [" & StreamUNIT & "] ?"
            .TextAlign = fmTextAlignRight
            .Height = 11
            .Width = LeftPos - 15
            .Left = 8
            .Top = TopPos + dH * 2 + 2.2  '2.2 is to align text with TextBox
            .Font.size = 9
            .BackColor = RGB(255, 255, 255)
            .AutoSize = False
        End With
'   Add the box for the "total" mass  input  --this is the info box for the TOTAL amount
'TextBox 1-to-(NumberOfStreams + 1)  :  for INPUT
        Set NewTextBox = MassPercForm.Controls.Add("forms.textbox.1")
        With NewTextBox
            .TextAlign = fmTextAlignCenter
            .Width = 50
            .Height = 15
            .Left = LeftPos
            .Top = TopPos + dH * 2
            .AutoSize = False
        End With
'   INPUT  stream total  TEMP  --this is the question Labelbox
'LabelBox (NumberOfStreams + 3) :  just a label
        Set NewLabelBox = MassPercForm.Controls.Add("forms.label.1")
        With NewLabelBox
            .Caption = "Stream Temperature ?"
            .TextAlign = fmTextAlignRight
            .Height = 10.5
            .Width = LeftPos - 15
            .Left = 8
            .Top = TopPos + 3 * dH + 2.2  '2.2 is to align text with TextBox
            .Font.size = 9
            .BackColor = RGB(255, 255, 255)
            .AutoSize = False
        End With
'   Add the box for the "total" temperature  input  --this is the info box
'TextBox 1-to-(NumberOfStreams + 2)  :  for INPUT
        Set NewTextBox = MassPercForm.Controls.Add("forms.textbox.1")
        With NewTextBox
            .TextAlign = fmTextAlignCenter
            .Width = 50
            .Height = 15
            .Left = LeftPos
            .Top = TopPos + dH * 3
            .AutoSize = False
        End With
'  this is the combobox  [C] or [F] for temperature
        Set NewComboBox = MassPercForm.Controls.Add("forms.ComboBox.1")
        With NewComboBox
            .Width = 34
            .Height = 15
            .Left = LeftPos + 55
            .Top = TopPos + dH * 3
            .RowSource = "Dimensions!A2:A3"
            .AutoSize = False
        End With
            
'put in the Total's box (SUMMATION  of the text boxes on the tempform) :  This Sum's-up the percentages from the "TextBox's"
'LabelBox (NumberOfStreams + 4) :  just a label
        Set NewLabelBox = MassPercForm.Controls.Add("forms.label.1")  'message "Total"
        With NewLabelBox
            .Caption = "Total:"
            .TextAlign = fmTextAlignCenter
            .Height = 10
            .Width = 25
            .Left = LeftPos
            .Top = TopPos - dH * 2 + 5
            .Font.size = 8
            .AutoSize = False
        End With
        
'   total text-box    this displays the  TOTAL VALUE  of the stream info   :  This Sum's-up the percentages from the "TextBox's"
'TextBox 1-to-(NumberOfStreams + 3)  :  for TOTAL VALUE (auto-sum)
        Set NewTextBox = MassPercForm.Controls.Add("forms.textbox.1")  'updating textbox as user inputs percentages
        With NewTextBox
            .Width = 80
            .Height = 15
            .Left = LeftPos
            .Top = TopPos - dH
            .SpecialEffect = fmSpecialEffectSunken
            .TextAlign = fmTextAlignCenter
            .BackColor = RGB(255, 255, 204)    'light yellow
            .AutoSize = False
        End With
        
'Adjust Main Form
    With MassPercForm  'Set-up main form
        '.Properties("Caption") = "Stream Information"
        .Caption = "Stream Information"
        .Width = LeftPos + 120
        If (TopPos + 7 * dH) > 380 Then  'if greater than 15 boxes
            .Height = 380  'limit to 15 boxes"
            
'ADD  SCROLLBAR  for large list
            .ScrollBars = 2 'vertical scroll bar on form
            .ScrollTop = 6 'top-position of the scroll bar
            .ScrollHeight = TopPos + 8 * dH 'top-position of the scroll bar
        Else
            .Height = TopPos + 8 * dH 'height from number of boxes wanted
        End If
    End With
'   Add the EXIT button
    Set NewComboBox = MassPercForm.Controls.Add("forms.CommandButton.1")
    With NewComboBox
        .Caption = "EXIT"
        .Height = 18
        .Width = 38
        .Left = dH
        .Top = TopPos + 4.5 * dH
    End With
    
    
'    Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("MassPercForm").CodeModule
 '   With VBCodeMod
 '           lineCount = .CountOfLines + 1
 '           .InsertLines lineCount, _
 '               "Private Sub EXITbutton" & "_Click()" & Chr(13) & _
'                "End" & Chr(13) & _
'                "End Sub" & Chr(13)
'    End With
    
        
'   Add the OKAY button
    Set NewComboBox = MassPercForm.Controls.Add("forms.CommandButton.1")
    With NewComboBox
        .Caption = "OKAY"
        .Height = 21
        .Width = 61
        .Left = LeftPos
        .Top = TopPos + 4.5 * dH
    End With
    
End Sub
 
Last edited by a moderator:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

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