Sub UserForm_Activate()
Dim txtBox As MSForms.Label
Dim rngFields As Excel.Range
Dim field As Excel.Range
Dim lngNextTop As Long
Dim lngTitleBarHeight As Long
Dim GenderOK As MSForms.CommandButton
Const cTextBoxHeight As Long = 18
Const cTextBoxWidth As Long = 30
Const cGap As Long = 4
Set rngFields = Excel.Range("Samples")
lngTitleBarHeight = Me.Height - Me.InsideHeight
lngNextTop = cGap
For Each field In rngFields
If field.Value <> "" Then
Set txtBox = Me.Controls.Add("Forms.Label.1", "txt")
txtBox.Caption = field.Value
txtBox.Left = cGap
txtBox.Top = lngNextTop + 4
txtBox.Height = cTextBoxHeight
txtBox.Width = cTextBoxWidth
txtBox.Font.Size = 8
txtBox.Font.Bold = True
Set ComboBox1 = Me.Controls.Add("Forms.ComboBox.1", "Gender")
With ComboBox1
.AddItem "Male"
.AddItem "Female"
.AddItem "Unknown"
.Left = txtBox.Width + cGap + 8
.Top = lngNextTop
.Height = cTextBoxHeight
.Width = 70
.Font.Size = 8
.Font.Bold = False
.ControlSource = Cells(field.row, field.column + 2).Address
End With
lngNextTop = lngNextTop + cTextBoxHeight + cGap
Me.Height = lngNextTop + lngTitleBarHeight
End If
Next field
Set txtBox = Nothing
Set GenderOK = Me.Controls.Add("Forms.CommandButton.1", "GenderOK")
With GenderOK
.Top = lngNextTop + 5
.Left = 42
.Width = 40
.Caption = "OK"
.Font.Size = 10
.Font.Bold = True
.Height = 20
End With
Me.Height = lngNextTop + 28 + lngTitleBarHeight
End Sub
Sub GenderOK_Click()
Range("G17").Value = "Sample genders set !"
Unload Me ' Close the form
End Sub
Bookmarks