I've got a userform on which I want to add a textbox and a combobox for each entry on the spreadsheet which meets certain criteria.
The code for adding them (see below) is working fine, but when I actually select an entry in the combobox, excel freezes completely, and requires a system reboot.
Any ideas? Is it something I'm doing wrong? And if so, how should I be doing it?
Sub Get_review_cases()
'all these variables (apart from endrow) are left over from the code I found on the internet, which used to generate the form itself on the fly. I don't quite know what they all are, so I haven't deleted any!
Dim MyUserForm As VBComponent
Dim NewOptionButton As Msforms.OptionButton
Dim NewCommandButton1 As Msforms.CommandButton
Dim NewCommandButton2 As Msforms.CommandButton
Dim MyComboBox As Msforms.ComboBox
Dim N, X As Integer, MaxWidth As Long
Dim formheight As Variant
Dim endrow As Variant
'the a variable sets the 'top' position of the boxes I'm adding
a = 6
endrow = Sheets("Sheet1").Range("a65536").End(xlUp).Row
For i = 4 To endrow
'checks if the date on the sheet is earlier than today, and that there's no entry in the other cell
If Worksheets("Sheet1").Cells(i, 10) < Now _
And Worksheets("Sheet1").Cells(i, 14) = "" Then
'adds a textbox with a reference number taken from the sheet
Set newbox = Dynamicform.Controls.Add("forms.Textbox.1")
With newbox
.Text = Worksheets("Sheet1").Cells(i, 2).Value
.Height = 15.75
.Width = 78
.Left = 6
.Top = a
End With
'adds a textbox with the customer's title, forename and surname
Set newbox = Dynamicform.Controls.Add("forms.Textbox.1")
With newbox
.Text = Worksheets("Sheet1").Cells(i, 3).Value & " " & _
Worksheets("Sheet1").Cells(i, 5).Value & " " & _
Worksheets("Sheet1").Cells(i, 3).Value
.Height = 15.75
.Width = 150
.Left = 90
.Top = a
End With
'adds a combobox and populates it with a list of contacts from sheet 'HEOs'
Set newbox = Dynamicform.Controls.Add("forms.Combobox.1")
With newbox
.ControlSource = "HEOs!A1"
.RowSource = "HEOs!A1:A3"
.Height = 15.75
.Width = 78
.Left = 244
.Top = a
End With
a = a + 24
Dynamicform.ScrollHeight = a
End If
Next i
Bookmarks