I built the code in simple excel file. It is working. However I am unable to add this code to my existing code. I will be happy to help with existing code if someone is willing to help.
I want to add following code in my existing code:
Option Explicit
Private Comb_Arrow As Boolean
Private Sub Locs_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
‘ Set flag if arrow keys are pressed
Comb_Arrow = (KeyCode = vbKeyUp) Or (KeyCode = vbKeyDown)
' Reload the full list if Enter key is pressed
If KeyCode = vbKeyReturn Then
With Me.Locs
.List = Worksheets("Sheet2").Range("A2", Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp)).Value
.DropDown ' Show the dropdown list
End With
End If
End Sub
Private Sub Locs_Change()
‘ Exit if the selected item is a placeholder or matches a specific cell value
If Me.Locs.Text = “Select a location” Or Me.Locs.Text = Setup.Cells(20, 1).Value Then
Exit Sub
End If
Dim i As Long
' Only process if the change was not caused by arrow keys
If Not Comb_Arrow Then
With Me.Locs
' Load the full list from the worksheet
.List = Worksheets("Sheet2").Range("A2", Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp)).Value
.ListRows = Application.WorksheetFunction.Min(4, .ListCount) ' Limit the number of visible rows
' Filter the list based on user input
If Len(.Text) > 0 Then
For i = .ListCount - 1 To 0 Step -1
If InStr(1, .List(i), .Text, vbTextCompare) = 0 Then
.RemoveItem i
End If
Next i
' Show the filtered list
.DropDown
End If
End With
' Call the additional procedure
Call goals
End If
End Sub
I tried following code :
Option Explicit
Private Comb_Arrow As Boolean
Private Sub Locs_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
‘ Set flag if arrow keys are pressed
Comb_Arrow = (KeyCode = vbKeyUp) Or (KeyCode = vbKeyDown)
' Reload the full list if Enter key is pressed
If KeyCode = vbKeyReturn Then
With Me.Locs
.List = Worksheets("Sheet2").Range("A2", Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp)).Value
.DropDown ' Show the dropdown list
End With
End If
End Sub
Private Sub Locs_Change()
‘ Exit if the selected item is a placeholder or matches a specific cell value
If Me.Locs.Text = “Select a location” Or Me.Locs.Text = Setup.Cells(20, 1).Value Then
Exit Sub
End If
Dim i As Long
' Only process if the change was not caused by arrow keys
If Not Comb_Arrow Then
With Me.Locs
' Load the full list from the worksheet
.List = Worksheets("Sheet2").Range("A2", Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp)).Value
.ListRows = Application.WorksheetFunction.Min(4, .ListCount) ' Limit the number of visible rows
' Filter the list based on user input
If Len(.Text) > 0 Then
For i = .ListCount - 1 To 0 Step -1
If InStr(1, .List(i), .Text, vbTextCompare) = 0 Then
.RemoveItem i
End If
Next i
' Show the filtered list
.DropDown
End If
End With
' Call the additional procedure
Call goals
End If
End Sub
Tushar Gohel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.