+ Reply to Thread
Results 1 to 1 of 1

VBA code: automplete a column from a list of items in a different sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    10-07-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    1

    VBA code: automplete a column from a list of items in a different sheet

    I would like to autocomplete columns G and H from lists in columns A and B of sheet 2, respectively. I am a biologist and know absolutely NOTHING about VBA. I found some code that should serve my purpose for one of the two items. But I have no idea how to adapt it to our sheets, not even to autocomplete just one column. I would be very grateful if someone can do that for me. The code is at http://www.vbaexpress.com/kb/getarticle.php?kb_id=244
    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
    Last edited by kakkerlat; 10-07-2013 at 03:24 PM. Reason: posted code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] List of Codes on one sheet, Billing data on another sheet, highlight code if in list
    By children in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-18-2013, 05:03 PM
  2. [SOLVED] List unmatched items in 3rd column & sum matched items in 4th column
    By sharonvining in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-12-2012, 03:24 PM
  3. [SOLVED] List Box can you colour code items or make bold
    By christopher ward in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2006, 07:55 PM
  4. [SOLVED] i want to delete duplicate items in a list using code.
    By ndm berry in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-27-2005, 10:05 AM
  5. How do I color code items in a drop down list?
    By Beckers1986 in forum Excel General
    Replies: 1
    Last Post: 01-23-2005, 10:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1