Try the code below. You may have to update the name of ComboBox1
.
To find the name:
- Select the combobox in Design Mode.
- Click the Format ribbon.
- In the Arrange section click the Selection Pane.
- All shapes, their names & visibility will appear in a separate pane.
This will work if your using a range to fill your combobox:
Sub DeleteShp()
Dim a As String
Dim rFound As Range
Dim LookupRange As Range
Set LookupRange = Range(Sheet1.ComboBox1.ListFillRange)
a = Trim(InputBox("Vælg fartøj der skal slettes"))
With LookupRange
Set rFound = .Find(What:=a, After:=.Cells(1, 1), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext)
If Not rFound Is Nothing Then
rFound.Delete Shift:=xlUp
End If
End With
End Sub
If you've populated your combobox using AddItem
:
Sub InitialiseShp()
'Fills combobox with "Item A" to "Item F".
Dim x As Long
Sheet1.ComboBox1.Clear
For x = 65 To 70
Sheet1.ComboBox1.AddItem "Item " & Chr(x)
Next x
End Sub
You'll need to search the items to return the correct index number to remove:
Sub DeleteShp_2()
Dim a As String
Dim x As Long
a = Trim(InputBox("Vælg fartøj der skal slettes"))
With Sheet1.ComboBox1
For x = 0 To .ListCount - 1
If .List(x) = a Then
.RemoveItem x
Exit For
End If
Next x
End With
End Sub