Well, that's no good.
You could .additem when you open the workbook (or activate the sheet).
Or you could just set up the .listfillrange to point at another worksheet
(hidden???).
I used Sheet2, A1:A3 for my testing.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim rng As Range
Dim Obj As OLEObject
Dim myListRng As Range
Application.ScreenUpdating = False
If Target.Count > 1 Then Exit Sub
If Target.Row < 17 Then Exit Sub
If Target.Column = 7 Then
Cells(Target.Row, "C").Select
If IsEmpty(Target) Then Exit Sub
With Me.Parent.Worksheets("sheet2")
Set myListRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))
End With
Target.Offset(, -6) = Date
Target.Offset(, -5).Font.Size = "12"
Target.Offset(, -5).Interior.Color = RGB(204, 204, 255)
Target.Offset(, -4).Interior.Color = RGB(204, 204, 255)
Target.Offset(, -4).Font.Size = "11"
Set rng = Cells(Target.Row + 1, "F")
' Check if there is already a checkbox
For Each Obj In ActiveSheet.OLEObjects
If TypeOf Obj.Object Is MSForms.CheckBox Then
If Obj.TopLeftCell.Address = rng.Address Then
Exit Sub
End If
End If
Next
Set Obj = ActiveSheet.OLEObjects.Add( _
ClassType:="Forms.CheckBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=rng.Left, _
Top:=rng.Top, _
Width:=rng.Width, _
Height:=rng.Height)
With Obj
.Object.Caption = ""
.LinkedCell = rng.Address
.Placement = xlMoveAndSize
.Object.BackColor = RGB(204, 204, 255)
.Object.Value = True
.Left = rng.Left
.Top = rng.Top
.Width = rng.Width
.Height = rng.Height
End With
Set rng = Me.Cells(Target.Row + 1, "E")
' Check if there is already a combobox
For Each Obj In ActiveSheet.OLEObjects
If TypeOf Obj.Object Is MSForms.ComboBox Then
If Obj.TopLeftCell.Address = rng.Address Then
Exit Sub
End If
End If
Next
Set Obj = Me.OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=rng.Left, _
Top:=rng.Top, _
Width:=rng.Width, _
Height:=rng.Height)
With Obj
.LinkedCell = rng.Address
.Placement = xlMoveAndSize
.Left = rng.Left
.Top = rng.Top
.Width = rng.Width
.Height = rng.Height
.ListFillRange = myListRng.Address(external:=True)
.Object.Style = fmStyleDropDownList
.Object.Value = myListRng(2).Value
End With
Set rng = Cells(Target.Row + 1, "H")
' Check if there is already a checkbox
For Each Obj In ActiveSheet.OLEObjects
If TypeOf Obj.Object Is MSForms.CheckBox Then
If Obj.TopLeftCell.Address = rng.Address Then
Exit Sub
End If
End If
Next
Set Obj = ActiveSheet.OLEObjects.Add( _
ClassType:="Forms.CheckBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=rng.Left, _
Top:=rng.Top, _
Width:=rng.Width, _
Height:=rng.Height)
With Obj
.Object.Caption = ""
.LinkedCell = rng.Address
.Placement = xlMoveAndSize
.Object.Value = False
.Object.BackColor = RGB(255, 204, 153)
.Left = rng.Left
.Top = rng.Top
.Width = rng.Width
.Height = rng.Height
End With
Set rng = Cells(Target.Row + 1, "J")
' Check if there is already a checkbox
For Each Obj In ActiveSheet.OLEObjects
If TypeOf Obj.Object Is MSForms.CheckBox Then
If Obj.TopLeftCell.Address = rng.Address Then
Exit Sub
End If
End If
Next
Set Obj = ActiveSheet.OLEObjects.Add( _
ClassType:="Forms.CheckBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=rng.Left, _
Top:=rng.Top, _
Width:=rng.Width, _
Height:=rng.Height)
With Obj
.Object.Caption = ""
.LinkedCell = rng.Address
.Placement = xlMoveAndSize
.Object.Value = False
.Object.BackColor = RGB(204, 204, 255)
.Left = rng.Left
.Top = rng.Top
.Width = rng.Width
.Height = rng.Height
End With
Cells(Target.Row, "I").Select
Application.ScreenUpdating = True
End If
End Sub
Thanks to your help, I got it almost worked out. I adjusted the code
slightly from the one that I showed you before, but basically it is
the same.
The problem I keep having is that when I save and exit the workbook
and then open it again, the values (text1, text2, etc) in the
comboboxes are at the value I entered before exiting and I cannot
adjust that anymore. I can edit the checkboxes though.
I am now using the following code and it seems to work fine, even with
sorting and zoom factor not at 100%:
<<snipped>>