Arnoud,
Hieronder de code die de huidige code moet vervangen.
Dan werkt de combobox ook voor kolom 1.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Column = 1 And (Target.Row >= 6 And Target.Row <= 7) Then
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
Target.Offset(0, 1).ClearContents
End If
End If
If Target.Column = 2 And (Target.Row >= 6 And Target.Row <= 7) Then
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, _
Cancel As Boolean)
Dim strV As Variant
Dim cboTemp As OLEObject
Dim ws As Worksheet
Dim rng As Range
Set ws = ActiveSheet
Set cboTemp = ws.OLEObjects("TempCombo")
On Error GoTo errHandler
'alleen cellen in kolom A en B krijgen een keuzelijst
If Target.Column <= 2 Then
With cboTemp
'clear and hide the combo box
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
If Target.Validation.Type = 3 Then
'if the cell contains
'a data validation list
Cancel = True
Application.EnableEvents = False
'get the data validation formula
strV = Target.Validation.Formula1
'vertalen str=
strV = VertalenValidatie(Target, strV)
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = strV
.LinkedCell = Target.Address
End With
cboTemp.Activate
'open the drop down list automatically
Me.TempCombo.DropDown
End If
End If
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
'=========================================
Private Sub TempCombo_LostFocus()
With Me.TempCombo
.Top = 10
.Left = 10
.Width = 0
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End Sub
'====================================
'Optional code to move to next cell
'if Tab or Enter are pressed
'from code by Ted Lanham
'***NOTE: if KeyDown causes problems,
'change to KeyUp
'Table with numbers for other keys
'such as Right Arrow (39)
'https://msdn.microsoft.com/en-us/library/aa243025%28v=vs.60%29.aspx
Private Sub TempCombo_KeyDown(ByVal _
KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
Select Case KeyCode
Case 9 'Tab
ActiveCell.Offset(0, 1).Activate
Case 13 'Enter
ActiveCell.Offset(1, 0).Activate
Case Else
'do nothing
End Select
End Sub
'====================================
Private Sub TempCombo_Change()
Dim strV As Variant
Dim v As Variant, i As Long
Dim rngL As Range
If Len(Me.TempCombo.LinkedCell) > 0 Then
Application.EnableEvents = False
Set rngL = Me.Range(Me.TempCombo.LinkedCell)
strV = rngL.Validation.Formula1
strV = VertalenValidatie(Me.Range(Me.TempCombo.LinkedCell), strV)
With Me.TempCombo
If .Value <> "" And .ListIndex = -1 Then
Select Case rngL.Column
Case 2
v = Worksheets("Productschema Zonder searchbox").Range(strV).Value
Case 1
v = Worksheets("Foodgroup combi").Range(strV).Value
rngL.Offset(0, 1).ClearContents
End Select
.ListFillRange = "" 'clear all items
'Repopulate with matched items
For i = LBound(v, 1) To UBound(v, 1)
If LCase(v(i, 1)) Like "*" & LCase(.Value) & "*" Then
.AddItem v(i, 1)
End If
Next i
Else
'Repopulate with all items
.ListFillRange = strV
Select Case rngL.Column
Case 1
rngL.Offset(0, 1).ClearContents
End Select
End If
End With
End If
Application.EnableEvents = True
End Sub
Function VertalenValidatie(Target As Range, strV As Variant) As Variant
'vertalen van de validatie formule
Set rng = Me.Range("F1") 'neem hier een cel die niet wordt gebruikt in je werkblad
Application.EnableEvents = False
rng.FormulaLocal = strV
strV = rng.Formula
rng.Clear
'INDIRECT moeten we nog kwijt
strV = Replace(strV, "INDIRECT", "")
strV = Replace(strV, "=", "")
'de formule uit kolom 2 moet geëvalueerd worden
'de formule uit kolom 1 niet
If Target.Column = 2 Then
strV = Application.Evaluate(strV)
End If
VertalenValidatie = strV
Application.EnableEvents = True
End Function
Jan