[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Microsoft Excel Combo Box Auto complete Help Required

Posted on 2014-07-30
12
Medium Priority
?
632 Views
Last Modified: 2014-08-05
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
0
Comment
Question by:Tahir2008
  • 7
  • 4
12 Comments
 
LVL 12

Expert Comment

by:James Elliott
ID: 40232616
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
0
 
LVL 1

Author Comment

by:Tahir2008
ID: 40232651
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

0
 
LVL 1

Author Comment

by:Tahir2008
ID: 40232654
How could the above be modified to allow the F2 function to edit the combo box as it currently does by double clicking?
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
LVL 1

Author Comment

by:Tahir2008
ID: 40234840
Any more feedback from anyone on this?
0
 
LVL 24

Accepted Solution

by:
Ejgil Hedegaard earned 2000 total points
ID: 40234873
Try this, made some time ago for something similar.
It is "Datavalidation as you type", or you can select the value.
Select a cell in the Provider range, and a box will pop up.
DataValidationAsYouType.xlsm
0
 
LVL 1

Author Comment

by:Tahir2008
ID: 40234878
Ejgill, that seems to do exactly what I need, its fab.. any background info on how to implement this?
0
 
LVL 24

Expert Comment

by:Ejgil Hedegaard
ID: 40235305
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.
0
 
LVL 1

Author Comment

by:Tahir2008
ID: 40235448
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
0
 
LVL 24

Expert Comment

by:Ejgil Hedegaard
ID: 40236051
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.
0
 
LVL 1

Author Comment

by:Tahir2008
ID: 40236329
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.
0
 
LVL 24

Expert Comment

by:Ejgil Hedegaard
ID: 40236580
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.
0
 
LVL 1

Author Closing Comment

by:Tahir2008
ID: 40241129
Thanks for the assistance, much appreciated, this did the trick.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question