+ Reply to Thread
Results 1 to 2 of 2

Thread: Code to allow autofill and Validation

  1. #1
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Code to allow autofill and Validation

    Basically i'm building a database in access with all the data handling done across a network in excel.(both 2007) Current problem is validation.

    The plan is to have code that takes a validated cell i.e. one that has a list associated with it from the excel built in validation tool and populate a temp combo box with that list, this means that every time a cell is selected a validated list can be chosen from, as well as this as the text is typed in the list is autocompleted!..

    i had this working for one sheet, but when i try to create a new instance of it in a new worksheet it fails..

    can anyone tell me why?

    thanks

    Jonathan

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim str As String
    Dim cboTemp As OLEObject
    Dim ws As Worksheet
    'Dim wsList As Worksheet
    Set ws = ActiveSheet
    On Error GoTo errhandler
    'Set wsList = Sheets("ValidationLists")
    
    
    If Target.count > 1 Then GoTo exitHandler
    
    Set cboTemp = ws.OLEObjects("TempCombo")
      On Error Resume Next
    If cboTemp.Visible = True Then
      With cboTemp
        .Top = 10
        .Left = 10
        .ListFillRange = ""
        .LinkedCell = ""
        .Visible = False
        .Value = ""
      End With
    End If
    
      On Error GoTo errhandler
      If Target.Validation.Type = 3 Then
        'if the cell contains a data validation list
        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 + 15
          .Height = Target.Height + 5
          .ListFillRange = str
          .LinkedCell = Target.Address
        End With
        cboTemp.Activate
      End If
    
    exitHandler:
      Application.ScreenUpdating = True
      Application.EnableEvents = True
      Exit Sub
    errhandler:
      Resume exitHandler
    
    End Sub
    '====================================
    'Optional code to move to next cell if Tab or Enter are pressed
    'from code by Ted Lanham
    Private Sub TempCombo_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
    update..

    turns out all that needs to be done was renaming a combo box "TempCombo".. not a clue how i missed this.. wasted the better part of a day.. anyway, feel free to use the code if it's helpful fo you, originally from http://www.contextures.com/
    Last edited by Jollyfrog; 09-28-2010 at 06:09 AM. Reason: solved!

  2. #2
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Code to allow autofill and Validation

    Hi Jonathan

    I tried to use this code two years ago and it kept locking up Excel (both 2000 and 2007). I'm glad to hear you got it working. I may have to revisit my efforts.

    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0