+ Reply to Thread
Results 1 to 22 of 22

Change Labels in Userform based on selection in ComboBox

  1. #1
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Change Labels in Userform based on selection in ComboBox

    I've created a UserForm that has a ComboBox (w/RowSource = a Range) from which you can select a Department. The UserForm also contains a list of 20 Labels, each with a TextBox next to it. These are tasks for which to enter hours (in the TextBox). See my example below. This is working fine. My challenge is this: Sheet 8 has 15 columns ("A" through "O") for different departments, each column has up to 20 items (tasks) listed below the columns header which contains the department names.
    How do I add an IF statement so when I select a different department (which is in Sheet8 column B for instance) from the ComboBox, the Sheet8.Range("A3").TEXT will change to Sheet8.Range("B3").Text

    Private Sub UserForm_Activate()
    txtStartDate.Value = Format(Date, "mm / d / yy")
    Label1.Caption = Sheet8.Range("A3").Text
    Label2.Caption = Sheet8.Range("A4").Text
    Label3.Caption = Sheet8.Range("A5").Text
    Label4.Caption = Sheet8.Range("A6").Text
    Label5.Caption = Sheet8.Range("A7").Text
    End Sub

    There are 20 labels, but I'm only showing the first few as an example. As you can tell, I'm a beginner at VBA, and any help will be greatly appreciated. Thank you!
    John

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change Labels in Userform based on selection in ComboBox

    John

    Why do you have this code in the form's Activate event?

    It would be better placed in the combobox's change event.

    Speaking of the combobox, how exactly did you populate it?

    Do you have a list of the departments somewhere?

    If you do does that list correspond to the data on Sheet8?

    For example if you select the first department in the combobox will the data for it be in the first column of Sheet8?

    Perhaps you could attach the file?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Change Labels in Userform based on selection in ComboBox

    Norie,
    Thanks for your prompt reply.

    I'm not sure how I would place this in the combobox's change event.

    The combobox is populated (using RowSource in the combobox properties) from a list of departments in another sheet (Sheet2). This list is for the user to create their company and set-up thei departments. These department names are copied/linked to the column headers in Sheet8. Sheet8 is basically a lookup table for all the tasks for each department.

    Yes. when you select the first Department from the Combobox, the data will be in the first column of Sheet8. The second will be in the second column ("B"), etc.

    I'm designing this workbook for someone else, and it contains some proprietary designs and data, so not sure if they want for me to share it. If needed, perhaps I can create a new workbook and extract only the sheets we're talking about.

    Thanks,

    john

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change Labels in Userform based on selection in ComboBox

    John

    When do you want the labels to change?

    If it's when a different deparment is selected from the combobox then the code should go in it's change event.

    Something like this perhaps.
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Change Labels in Userform based on selection in ComboBox

    Norie,

    Thank you. This is almost working, but I keep getting a "Cannot find the specified Object" error in the Me.Controls line.
    All my labels are named by number, Label1, Label2, etc. May that be the issue?
    When I change the "Label" to "Label1" (in the code you'd sent me), I still get the same error but when I move my mouse over the line of code, I can see the correct result for the first Label.

    Also, the headers (Department Names) in Sheet8 are in row 2. The list of tasks starts in row 3.

    John
    Last edited by johnw993; 01-02-2013 at 07:30 PM.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change Labels in Userform based on selection in ComboBox

    John

    I assumed the labels were named Label1, Label2 etc and that's what I based part of the code on.

    It might be worth double checking the names.

    Also, in your example the data you were using for the label captions appears to start in row 3, so that's where I've started.

    Should the code include row 2?

  7. #7
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Change Labels in Userform based on selection in ComboBox

    Norie,

    The Labels start with Label15 (which I've tried to use in your code) and end with Label29. This is what I have, and the Me.Controls line of code highlights (yellow) with the Debug tool. But, as I said before, when I move my moue over that line, I can see the correct value returned.

    Private Sub ComboBox_Change()
    Dim idx As Long
    Dim I As Long

    idx = ComboBox.ListIndex

    If idx <> -1 Then
    With Sheet8
    For I = 3 To .Range("A" & Rows.Count).Offset(, idx).End(xlUp).Row
    Me.Controls("Label15" & I - 2).Caption = .Range("A" & I).Offset(0, idx).Text
    Next I
    End With
    End If

    End Sub

    I'm still getting the same run-time error. "Cannot find specified object".

    btw: thank you so much for your time in helping out with this.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change Labels in Userform based on selection in ComboBox

    John

    I've assumed your labels are named Label1, Label2 etc not Label15, Label16 etc.

    Try this.
    Please Login or Register  to view this content.
    PS Can you use code tags when posting code? There's a link in my signature that should explain how to use them.

  9. #9
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Change Labels in Userform based on selection in ComboBox

    Aha!!
    It is working! For all Departments selected, except the first one. That one still gives me the same error. I've changed I=3 to I=2, but that didn't do it.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change Labels in Userform based on selection in ComboBox

    I don't see why that would happen, the same labels are used for each department.

    You know what would make this a whole lot easier - a sample workbook.

    Then, hopefullly, there'll be no need to guess things.

  11. #11
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Change Labels in Userform based on selection in ComboBox

    Apparently Excel Forum was off-line for a few hours. Hereby attached a greatly condensed version of my workbook (deleted about 15 sheets, which interestedly enough made your code work... hmm).

    Even though I followed all instructions, I can't see the attachment, so please let me know if you received it or not.

    If you have the time and wherewithal, try entering some data into the user form and you'll see the error messages that appear.

    btw: there are two user forms. Only use Userform1, the frmWorkTime is an old design that will be replaced by UserForm1 (once I get it working). And the links to Active sheet will change from TestType to WorkDB.

    Again, thanks for all your help. Already you've been instrumental! If not a life saver
    Attached Files Attached Files
    Last edited by johnw993; 01-03-2013 at 01:07 AM.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    John

    The attachment does appear to be there but I won't have a chance to download and look at it until later.

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change Labels in Userform based on selection in ComboBox

    John

    There are 3 forms, which one should I be looking at?

  14. #14
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Change Labels in Userform based on selection in ComboBox

    UserForm1 is the one. Once it is working properly (which I think it now is) it will replace the frmWorkTime form.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change Labels in Userform based on selection in ComboBox

    So the form in the workbook is working?

    Does that mean this thread is solved?

  16. #16
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Change Labels in Userform based on selection in ComboBox

    No yet. I still get an error when I try to submit data using UserForm1.
    As you can see in the Userform, I'm using Label 15 through 29, and TextBox 1 through 15. I believe that is where the problem is. In the code below Label is not identified properly (I think):


    [Code]
    Private Sub cmdOK3_Click()

    Dim rng As Range

    Set rng = ActiveWorkbook.Sheets("WorkDB").Range("A" & Rows.Count).End(xlUp).Offset(1)

    For I = 1 To 20
    If Me.Controls("TextBox" & I).Value <> "" Then
    rng.Value = cboName1.Value
    rng.Offset(0, 1) = ComboBox.Value
    rng.Offset(0, 2).Value = Me.Controls("Label" & I).Caption
    rng.Offset(0, 3).Value = txtStartDate.Value
    rng.Offset(0, 4).Value = Me.Controls("TextBox2" & I).Value
    Set rng = rng.Offset(1)
    End If
    Next I

    End Sub
    [Code/]

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change Labels in Userform based on selection in ComboBox

    Have a look at the attached file.

    PS Good try at the code tags, not quite right though. Why not let the board do it for you?

    Select the code and press the # button above.
    Attached Files Attached Files
    Last edited by Norie; 01-03-2013 at 03:19 PM.

  18. #18
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Change Labels in Userform based on selection in ComboBox

    Norie,

    Thank you for editing the UserForm code. However I'm still running into an issue. Cannot Find the specified Object and the debugger highlights the last line in the code below (TextBox2).
    When running the form it populates the name, department and type of work in the database, but not the hours. It stops right there and shows the error message.


    Please Login or Register  to view this content.
    Last edited by johnw993; 01-03-2013 at 04:52 PM.

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Change Labels in Userform based on selection in ComboBox

    John

    Where exactly did the code stop?

    PS Did you have a shifty at the userform (UserForm2?) with the listbox?

  20. #20
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Change Labels in Userform based on selection in ComboBox

    It appears to stop where the hours entered in the UserForm need to be applied to the database. The Name, Department and Type of work are populating the rows in the database, but then it stops. So it does recognize the hours entered in the textboxes, because it only populates rows in the database for the type of work for which I've entered a value (hours).

    I hope I am showing it correctly in the code sample below. I tried to highlight the line that shows bright yellow when I choose debug on the error message (RunTime error, Could not find the specified object).
    btw: the first TextBox is TextBox1, not 2 as shown in the code. However if I change that to 1, I get the same error.

    UserForm2 was just a testform. The ListBox is gone (what is a shifty?). I've just copied and pasted the code we're using in UserForm1 into Userform2 (and changed all the names, references, etc.), since Userform2 will function very similar, except that it deals with Key-Indicators instead of Types of Work, and populates a different database. The form is working, except that I'm getting the exact same error message in UserForm2 as in UserForm1. See below:


    Please Login or Register  to view this content.
    btw: I've learned more from you in the last day and a half, then I have reading the entire VBA for Excel book. Thank you!
    Last edited by johnw993; 01-03-2013 at 07:13 PM.

  21. #21
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Change Labels in Userform based on selection in ComboBox

    Instead of sending you just an excerpt from the UserForm1 code, here is the full code. Basically what you have provided, with some minor edits.



    Please Login or Register  to view this content.
    If we can't resolve the error bug I'm experiencing by having you look at this code, I'm open to sending you the entire Workbook.

  22. #22
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Change Labels in Userform based on selection in ComboBox

    Nori,

    By removing the number from the Textbox2 statement, things appear to be working. Thank you for all your help. I'm closing this Thread.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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