Consulting

Results 1 to 4 of 4

Thread: Solved: Making a Conditional Combobox from Excel data

  1. #1
    VBAX Regular
    Joined
    Jan 2006
    Posts
    24
    Location

    Solved: Making a Conditional Combobox from Excel data

    I've setup a combobox on a Userform to extract data from an Excel worksheet using the below code

    [VBA]Private Sub UserForm_Initialize()
    'define name codes
    Dim ObjExcel As New Excel.Application
    Dim wb As Excel.Workbook
    Dim FName As Variant
    Dim x As Long
    Dim LastRow As Long

    FName = "M:\TEMPLATES\NAME_CODES.xls"

    Set wb = ObjExcel.Workbooks.Open(FName)

    With wb.Sheets(1)
    LastRow = .Range("C60").End(xlUp).Row
    For x = 1 To LastRow
    frmGetName.cbxType.AddItem (.Range("C" & x).Text)
    Next x
    End With

    frmGetName.cbxType.SetFocus
    End Sub[/VBA]

    All works fine and the combobox is populated OK with the data from Excel.

    What is the best way to make sure the user selection is from one of the loaded values (ie not just free text)?

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    [vba]ComboBox1.MatchRequired = True[/vba]requires the value of the control match an item in the list. If the user types in text that does not match an item, there is an error message. "Invalid property".

    BTW: you need to use the instruction when you Initialize the userform.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by fumei
    BTW: you need to use the instruction when you Initialize the userform.
    ...or you can set it in the properties window for the combobox
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    Jan 2006
    Posts
    24
    Location
    Thanks!

    Once again your help has saved me

Posting Permissions

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