Results 1 to 13 of 13

Thread: [RESOLVED] Automatically change textbox data accourding to combobox selected Item

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    42

    Resolved [RESOLVED] Automatically change textbox data accourding to combobox selected Item

    I have some employee Names on column "A" and relevant employee Numbers on column "B" in sheet1. On userform I have a combobox that shows employee names,i want when a name is selected on combobox his/her employee Number shown on a nearby txtbox.
    I know a code
    Code:
    Option Explicit
    
    Private Sub cboNames_Change()
    
    txtEmployeeNumber.Value = Worksheets("Sheet4").Cells(cboNames.ListIndex + 1, 2)
    
    End Sub
    
    '=========================================================================
    
    Private Sub UserForm_Initialize()
    
    Dim LastRow As Long
    
    cboNames.Clear
    With Worksheets("Sheet4") '<--replace "Sheet4" with the sheet you have your employees data
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        cboNames.List = Application.Transpose(.Range("A2:A" & LastRow).Value)
    End With
    
    End Sub
    I've put it in Userform module But it gives Compile error "Variable not defined" on this line
    Code:
    txtEmployeeNumber.Value = Worksheets("Sheet4").Cells(cboNames.ListIndex + 1, 2)

  2. #2
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: Automatically change textbox data accourding to combobox selected Item

    txtEmployeeNumber.Value

    Textboxes do not have a .Value property

    Try txtEmployeeNumber.Text

  3. #3
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: Automatically change textbox data accourding to combobox selected Item

    Private Sub cboNames_Change()

    txtEmployeeNumber.Value = Worksheets("Sheet4").Cells(cboNames.ListIndex + 1, 2)

    End Sub



    You have a sub called cboNames_Change so that tells me you have a combobox called cboNames so I don't believe that you can have a compile error "Variable not defined" as you say for this control

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: Automatically change textbox data accourding to combobox selected Item

    Just as a tip, when a moderately sophisticated line like the one you posted at the bottom of your #1 post gives you an error, and you can't get it sorted, break it into several lines.

    The following is rather extreme, but it gives you the idea:

    Code:
    
        Dim rng As Range
        Dim idx As Long
        Dim sVal As String
    
        idx = cboNames.ListIndex + 1
        If idx <> -1 Then
            Set rng = Worksheets("Sheet4").Cells(idx + 1, 2) ' <---- Hopefully, Worksheets has been set.
            sVal = rng.Value
            txtEmployeeNumber.Value = sVal ' <---- This line will fail because TextBox has no Value property.
        End If
    
    
    Regards,
    Elroy
    Last edited by Elroy; Feb 19th, 2017 at 01:13 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Automatically change textbox data accourding to combobox selected Item

    I don't know what you're doing wrong, mort, but here is a quick example using basically what you are doing, and it works just fine:

    Code:
    Option Explicit
    Dim AppExcel As Excel.Application
    Dim objWb As Excel.Workbook
    
    Private Sub cboNames_Click()  'NOTICE-I used the CLICK EVENT, not the CHANGE EVENT IN THE ComboBox, cboNames
        txtEmployeeNumber.Text = objWb.Worksheets(1).Cells(cboNames.ListIndex + 1, 2) 'NOTICE THE .Text, not .Value (as stated by others above)  '''also NOTE: I only have one worksheet in my workbook, hence (1).
    End Sub
    
    
    
    Private Sub Form_Load()
        cboNames.AddItem ("John")
        cboNames.AddItem ("Sam")
        cboNames.AddItem ("Sammi")
        cboNames.AddItem ("Bill")
        cboNames.AddItem ("James")
        Set AppExcel = CreateObject("Excel.Application")
        Set objWb = AppExcel.Workbooks.Add
        Set objWb = GetObject(App.Path & "\testfile.xlsx")  
    End Sub
    My Excel file (textfile.xlsx) looks like this:
    Column A Column B
    John 1
    Sam 2
    Sammie 3
    Bill 4
    James 5

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Automatically change textbox data accourding to combobox selected Item

    This isn't a VB question. The posted snippets make it clear this is Office VBA and the thread is in the wrong forum.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Automatically change textbox data accourding to combobox selected Item

    Indeed. Thread moved to Office Development.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    42

    Re: Automatically change textbox data accourding to combobox selected Item

    Code:
    Option Explicit
    Dim AppExcel As Excel.Application
    Dim objWb As Excel.Workbook
    
    Private Sub cboNames_Click()  'NOTICE-I used the CLICK EVENT, not the CHANGE EVENT IN THE ComboBox, cboNames
        txtEmployeeNumber.Text = objWb.Worksheets(1).Cells(cboNames.ListIndex + 1, 2) 'NOTICE THE .Text, not .Value (as stated by others above)  '''also NOTE: I only have one worksheet in my workbook, hence (1).
    End Sub
    
    Private Sub Form_Load()
        cboNames.AddItem ("John")
        cboNames.AddItem ("Sam")
        cboNames.AddItem ("Sammi")
        cboNames.AddItem ("Bill")
        cboNames.AddItem ("James")
        Set AppExcel = CreateObject("Excel.Application")
        Set objWb = AppExcel.Workbooks.Add
        Set objWb = GetObject(App.Path & "\testfile.xlsx")  
    End Sub
    Regardless changing parameter to suit my project again it gives the same error at the first step when i debug
    I think there's a problem with my workbook, Of course it is not perfect yet,I'm trying to complete my project step by step Although when i apply for debug everything seems ok. I try to attach the file here please spend 5 -10 min to check it
    Thanks

    Edit: I cant upload my file cuz the invalid extension. Is it possible to mail someone who like to help?If yes please inform me
    [email protected]
    Last edited by mortezataheri; Feb 20th, 2017 at 03:36 AM.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Automatically change textbox data accourding to combobox selected Item

    I cant upload my file cuz the invalid extension
    zip first
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    42

    Re: Automatically change textbox data accourding to combobox selected Item

    zip first
    I changed to zip by win zip but still is unacceptable,cuz it chanhed to rar not to zip
    Last edited by mortezataheri; Feb 20th, 2017 at 04:37 AM.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Automatically change textbox data accourding to combobox selected Item

    rar should be acceptable but most will not open

    i tested in your file
    Code:
    Private Sub cboName_Click()
    txtEmployeeNumber.Value = Worksheets("Sheet1").Cells(cboName.ListIndex + 2, 2)
    End Sub
    this works for me, you have the name for your combobox incorrect, in the OP
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    42

    Re: Automatically change textbox data accourding to combobox selected Item

    I tested in your file
    this works for me
    you mean that code worked on your own file or after fixing my file tested in mine then it worked?
    you have the name for your combobox incorrect, in the OP
    What's the meaning of OP ? irf you mean Original post, i changed parameters to suit my file then tested

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    42

    Re: Automatically change textbox data accourding to combobox selected Item

    This code works:
    Code:
    Private Sub cboName_Change() '< your combobox
        Dim EName As String
        Dim Row As Integer
        EName = Me.cboName.Text
        If EName <> "" Then
            With Application.WorksheetFunction
                Row = .Match(EName, Sheets("sheet1").Range("A2:A100"), 0) '< your data worksheet and range 
                txtEmployeeNumber.Text = .Index(Sheets("sheet1").Range("B2:B100"), Row) '< your textbox
                txtShift.Text = .Index(Sheets("sheet1").Range("C2:C100"), Row)
            End With
        End If
    End Sub
    I will tick as solved
    Thanks to all
    Last edited by mortezataheri; Feb 20th, 2017 at 02:33 PM.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width