Excel

"Autocomplete" using cells from different column

Ease of Use

Intermediate

Version tested with

97, 2002, 2003 

Submitted by:

byundt

Description:

Simulates the AutoComplete feature, but allows use of data from a different column or worksheet as the source. 

Discussion:

AutoComplete feature is limited to strings in the same column. For ease of data entry (especially in a template), the most common strings can be stored in a separate worksheet. The macro will search that table and return the matching string as soon as you hit the Enter key. 

Code:

instructions for use

			

Private Sub Worksheet_Change(ByVal Target As Range) 'Sub "autocompletes" data entered into column A using a source table on a different worksheet. If more than one match is ' found, the user is allowed to continue entering characters until a unique match is found. If no matches are found, the ' data is accepted as entered. ALT + Enter, Enter to force the macro to accept data as entered. The sub is triggered by ' the Enter key. Dim cel As Range, match1 As Range, match2 As Range, rg As Range, targ As Range '***Please adjust the next two statements before using this code!*** Set targ = Intersect(Target, Range("A:A")) 'Watch the cells in column A Set rg = Worksheets("Source data").Range("AutoCompleteText") 'Use named range AutoCompleteText for "autocomplete" info If targ Is Nothing Then Exit Sub Application.ScreenUpdating = False Application.EnableEvents = False On Error GoTo errhandler 'If code encounters an error, turn events back on For Each cel In targ If Not IsError(cel) Then If cel <> "" And Right(cel, 1) <> Chr(10) Then Set match1 = Nothing Set match1 = rg.Find(cel & "*", lookat:=xlWhole, MatchCase:=False) 'Match is case insensitive If Not match1 Is Nothing Then Set match2 = rg.FindNext(after:=match1) If match2.Address = match1.Address Then 'Code is fooled by identical strings in two cells cel = match1 'Only one match found. Use it to "autocomplete" the cell Else 'More than one match found. User must enter more data. Return to "Edit" mode cel.Activate Application.SendKeys ("{F2}") 'Begin editing after last character entered End If Else 'No matches found. Do not change entered text End If Else 'Strip the line feed from the end of the text string If cel <> "" And Right(cel, 1) = Chr(10) Then cel = Left(cel, Len(cel) - 1) End If End If Next cel errhandler: Application.EnableEvents = True On Error GoTo 0 Application.ScreenUpdating = True End Sub

How to use:

  1. Copy above code.
  2. Right-click the sheet tab of the worksheet that needs the "autocomplete" feature
  3. Choose "View Code" from the resulting pop-up
  4. Paste code into the resulting code pane
  5. Within the sub, change the definition of variable targ (from column A) to match the range of cells that needs the "autocomplete" feature
  6. Still within the sub, change the definition of variable rg (from range AutoCompleteText) to match the source of "autocomplete" information
  7. Press Alt + Q to close the VBE.
  8. Save workbook before any other changes.
 

Test the code:

  1. To test the code using the sample workbook, try typing the beginning of a boy's or girl's given name anywhere in column A, then hit the Enter key.
  2. If there is a unique match in the source table, then you should see the completed name as a result. The autocomplete source range contains the 2010 most popular names, so you are likely to get a hit when you type enough letters.
  3. If there is more than one match, you will be returned to "Edit" mode, and allowed to enter one or more additional characters to generate a unique match.
  4. If there is no match, then your data will be accepted as entered.
  5. If you want to force the macro to accept your data as entered (even though there are one or more matches), then ALT + Enter followed by Enter at the end of your text. The code will strip the line feed character generated by the ALT + Enter.
 

Sample File:

AutoComplete.zip 45.52KB 

Approved by mdmackillop


This entry has been viewed 244 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express