Link to home
Start Free TrialLog in
Avatar of Tahir2008
Tahir2008

asked on

Microsoft Excel Combo Box Auto complete Help Required

Hi,
I currently have a Microsoft Excel document which uses data validation lists pulled from another worksheet, these work perfectly fine. I needed the ability to auto complete the data validation list entries as they were being added to speed up data entry so found on the internet via Google a work around which uses a Combo Box overlay above the Data Validation and then some VB code to allow you to link the two together and use the auto complete.

The problem I have is, the above works fine when you double click the cell which has a drop down validation list as when you do this the edit mode is through the Combo Box and the auto complete works. However, if you tab across to the cell or press F2 to edit the box it edits the Drop Down validation box and the auto complete fails to work.

Is there any way I can modify this so I can tab across to the cell and press F2 to edit or just edit as a normal cell so the auto complete works and it edits via the Combo Box instead of the Data Validation drop down list?

The example I used was from the following website:

http://www.contextures.com/xlDataVal11.html

Any assistance with the above would be really appreciated.

Regards

Tahir
Avatar of James Elliott
James Elliott
Flag of United Kingdom of Great Britain and Northern Ireland image

The easiest thing would be to lock the cell and when locking the sheet, disable selection of locked cells. Then surround whatever code updates the cell with:

Activesheet.unprotect "yourPassword"
'your code that updates the cell value
Activesheet.protect "yourPassword"

Open in new window


If you need precisely the solution in your question then it would relatively easy to get the tab key to move into the combo box instead of the cell, but the F2 key would take a relatively elaborate hook into windows itself.

Rgds

Jell
Avatar of Tahir2008
Tahir2008

ASKER

The code that is being used at present is the following:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
  Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet

Set cboTemp = ws.OLEObjects("AutocompleteCombo")
  On Error Resume Next
  With cboTemp
  'clear and hide the combo box
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
  End With
On Error GoTo errHandler
  If Target.Validation.Type = 3 Then
    'if the cell contains a data validation list
    Cancel = True
    Application.EnableEvents = False
    'get the data validation formula
    str = Target.Validation.Formula1
    str = Right(str, Len(str) - 1)
    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 = str
      .LinkedCell = Target.Address
    End With
    cboTemp.Activate
    'open the drop down list automatically
    Me.AutocompleteCombo.DropDown

  End If
  
errHandler:
  Application.EnableEvents = True
  Exit Sub

End Sub
'=========================================
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
Application.EnableEvents = False
Application.ScreenUpdating = True

If Application.CutCopyMode Then
  'allow copying and pasting on the worksheet
  GoTo errHandler
End If

Set cboTemp = ws.OLEObjects("AutocompleteCombo")
  On Error Resume Next
  With cboTemp
    .Top = 10
    .Left = 10
    .Width = 0
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
    .Value = ""
  End With

errHandler:
  Application.EnableEvents = True
  Exit Sub

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

Private Sub AutocompleteCombo_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

Open in new window

How could the above be modified to allow the F2 function to edit the combo box as it currently does by double clicking?
Any more feedback from anyone on this?
ASKER CERTIFIED SOLUTION
Avatar of Ejgil Hedegaard
Ejgil Hedegaard
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ejgill, that seems to do exactly what I need, its fab.. any background info on how to implement this?
There are 2 named ranges on the sheets, one maintained by the code, a named userform (the pop up), a worksheet selection change event macro, and a workbook sheet change event macro.
The names are used in the code.

If you are familiar with VBA you can export/import/copy that to your workbook, and change the names and ranges to something that makes sense for the use in that workbook.

If not, I suggest you upload a workbook, then I can do the implementation for you.
If that is not possible (confidential), perhaps we can use an e-mail solution, if that is acceptable.
Can you tell me what this code does under the workbook? Is it needed?

Option Explicit

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim myRange As Range, mySortRange As Range
If Sh.Name = "Table" Then
    Set myRange = Range(Range("A1"), Range("A" & Cells.Rows.Count).End(xlUp))
    Set mySortRange = myRange.Offset(1).Resize(myRange.Rows.Count - 1, 1)
    ThisWorkbook.Worksheets("Table").Sort.SortFields.Clear
    ThisWorkbook.Worksheets("Table").Sort.SortFields.Add Key:=mySortRange, _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Table").Sort
        .SetRange myRange
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ThisWorkbook.Names("ProviderList").RefersTo = "=Table!" & myRange.Address
End If

End Sub
The code sort the validation list on sheet Table when changes are made, and update the range name (ProviderList) reference.

Yes it is needed to make it dynamic.
If the list is static the code can be removed.
But if I have a dynamic named range then it seems to update the list details correctly when changes are made without the code, this is the reason I asked.
Yes, that will work, but the code does more than update the range for the name.

If values are changed, or added below, the values are sorted to display in the correct order for the data validation.
Even values added anywhere in the column will get in the list, but then the range should be set once again, just before ThisWorkbook.Names... to eliminate blanks at the end of the validation list.

Using a dynamic named range, and manually sort the list will do the same.
Thanks for the assistance, much appreciated, this did the trick.