+ Reply to Thread
Results 1 to 30 of 30

Loop through names of buttons on a userform

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Loop through names of buttons on a userform

    Hi I need to simplify my code. Ive got a user form with 256 text boxes on, one for each column.
    I've named the text boxes "TbA", TbB", "TbC" etc to corespond to the column names.
    I've then got another text box named "A", "B", "C" etc which fills with the cell value from the current row.
    Is there a way of looping these instead?

    Private Sub CBNames_Change()
        Dim lRw As Long
        With Me
            lRw = .CBNames.ListIndex + 2    '<-add 2 to allow for header row & ListIndex starts at zero
            .A.Value = Sheets("Main").Cells(lRw, 1).Value
            .C.Value = Sheets("Main").Cells(lRw, 3).Value
            .D.Value = Sheets("Main").Cells(lRw, 4).Value
            .E.Value = Sheets("Main").Cells(lRw, 5).Value
            .F.Value = Sheets("Main").Cells(lRw, 6).Value
            'etc
        End With
    End Sub

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Loop through names of buttons on a userform

    maybe something like
    Private Sub CBNames_Change()
    Dim ctl As Control, lRw As Long
    lRw = 2
    With Sheets("Main")
        For Each ctl In Me.Controls
            If TypeName(ctl) = "TextBox" Then
                ctl.Value = .Cells(lRw, ctl.Name).Value
            End If
        Next ctl
    End With
    End Sub
    256 textboxes - it's a bit much

    Edited:
    And for this thread you can try
    ctl.Value = Format(.Cells(lRw, ctl.Name).Value, "dd.mm.yyyy")
    Last edited by nilem; 06-10-2012 at 09:12 AM.

  3. #3
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    Thanks for replying but I get type mismatch error on "ctl.Value = .Cells(lRw, ctl.Name).Value"

  4. #4
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    Tracker3.xlsHere's the workbook. Click command button2 on menu2

  5. #5
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Loop through names of buttons on a userform

    try
    Private Sub CBNames_Change()
    Dim ctl As Control, lRw As Long
    lRw = Me.CBNames.ListIndex + 2
    With Sheets("Main")
        For Each ctl In Me.Controls
            If TypeName(ctl) = "TextBox" Then
                If Len(ctl.Name) < 3 Then ctl.Value = .Cells(lRw, ctl.Name).Value
            End If
        Next ctl
    End With
    End Sub

  6. #6
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    Brilliant! Thank you that save a huge amount of typing! If you could find a way to make sure the date format was dd/mm/yy that would br great?

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Loop through names of buttons on a userform

    Are you really having 256 TextBoxes with s56 columns of data? I think your project is very poorly thought out, that amount of columns is going to be horrendous to manage

    If one collection is just the column names then use Labels not TextBoxes.

    Rename the labels as lbl1. lbl2 etc. Rename the TextBoxes as TB1,TB2 etc

    Private Sub CBNames_Change()
        Dim lRw As Long
        Dim iX As Integer
        With Sheets("Main")
            lRw = .CBNames.ListIndex + 2    '<-add 2 to allow for header row & ListIndex starts at zero
            For iX = 1 To .UsedRange.Columns.Count
                Me("lb" & iX).Caption = .Cells(1, iX).Value
                Me("Tb" & iX).Value = .Cells(lRw, iX).Value
            Next iX
        End With
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  8. #8
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    Yeah I know its a lot but I currently have 205 lots of training that a colleague can complete. I can now have one user form that lists each one and also I can edit the column title from the user form as well as report the date the colleague completed the training. Unfortunatly I'm trying to make it as user friendly as possible for people with even less Excel knowledge than me (if thats possible!) I really appreciate all the help you guys are giving me.

  9. #9
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Loop through names of buttons on a userform

    maybe so
    Private Sub CBNames_Change()
    Dim ctl As Control, lRw As Long
    lRw = Me.CBNames.ListIndex + 2
    With Sheets("Main")
        For Each ctl In Me.Controls
            If TypeName(ctl) = "TextBox" Then
                If Len(ctl.Name) < 3 Then
                    With .Cells(lRw, ctl.Name)
                        If IsDate(.Value) Then ctl.Value = _
                           Format(.Value, "dd/mm/yy") Else ctl.Value = .Value
                    End With
                End If
            End If
        Next ctl
    End With
    End Sub

  10. #10
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    I've now hit another problem - If I use ctrl.name as a variable, when I come to column "AS" I can't use that as a textbox name.
    Is there a better way to select a user from a combo box then have all his training listed? I can live without being able to edit the column titles

  11. #11
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Loop through names of buttons on a userform

    Have you tried what I suggested?

  12. #12
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    Hi roy I get an error "object does not support this property or method"

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Loop through names of buttons on a userform

    Have you replaced the TextBoxes used for Columns with Labels?

  14. #14
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    Tracker4.xlsHere you go "Menu2" commandbutton3

  15. #15
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    yes ill upload my code in sec

  16. #16
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    Sorry try that again Tracker4.xls

  17. #17
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Loop through names of buttons on a userform

    can't you just use a listbox to show the column data-then when the user selects a column there is just one text box to edit the value?
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  18. #18
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    Hi joseph, I'm not sure I understand how to use a list box for this. What I'm trying to do is somehow select a colleague from column B which then would list all the types of training he's completed (these are in the column titles) from row 1 but making sure that if a column was moved or deleted everything would still work. At the moment commandbutton2 works fine alowing me to not only see a list of column titles but also the date my chosen colleague completed it. The problem occurs because I used the textbox name as a variable and going through alphabetically I get to "AS" which cant be used as a textbox name of course. Maybe what I need is a new aproach but I can't see the wood for the trees! Thanks

  19. #19
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Loop through names of buttons on a userform

    in your Ind2 form replace the label and textbox with a listbox (LIstbox1) and change the code to
    Private Sub CBNames_Change()
        Dim lRw As Long
        Dim iX As Long
        Dim wsMain As Worksheet
        Set wsMain = Sheets("Main")
        lRw = Me.CBNames.ListIndex + 2    '<-add 2 to allow for header row & ListIndex starts at zero
            For iX = 1 To wsMain.UsedRange.Columns.Count
                With Me.ListBox1
                    .AddItem wsMain.Cells(1, iX).Value
                    .List(.ListCount - 1, 1) = wsMain.Cells(lRw, iX).Value
                End With
            Next iX
    End Sub

  20. #20
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    Thanks Joseph I see what you mean that is a lot easier and looks much better. The only thing is I need to also have the list show the dates the training was completed along side the list and then have some way to print the whole lot out. I really appreciate your help.

  21. #21
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Loop through names of buttons on a userform

    forgot to mention-set the listbox's columncount property to two-then you'll see the dates. :-)

    to printout I would use a preformatted worksheet that you can populate form the listbox-userforms generally do not print well.
    Last edited by JosephP; 06-10-2012 at 04:24 PM.

  22. #22
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    columncount is set to 1 but no dates

  23. #23
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Loop through names of buttons on a userform

    sorry-my bad. I meant 2 not True. ;-)

  24. #24
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    Sorry while youre being so kind how do I make the dates dd/mm/yy instead of mm/dd/yyyy Please

  25. #25
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    :-) brilliant! It looks so much better just the job thanks again. I assume I can take the data from the list box some how and send to a new sheet (sorry remember I'm a total newbie!)

  26. #26
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Loop through names of buttons on a userform

    yeah-something like this
    Private Sub CommandButton1_Click()
        Dim wsTemp As Worksheet
        Set wsTemp = Sheets.Add
        With Me.ListBox1
            wsTemp.Range("A1").Resize(.ListCount, .ColumnCount).Value = .List
        End With
        wsTemp.PrintOut
        Application.DisplayAlerts = False
        wsTemp.Delete
        Application.DisplayAlerts = True
    End Sub

  27. #27
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Loop through names of buttons on a userform

    for the dates change
    .List(.ListCount - 1, 1) = wsMain.Cells(lRw, iX).Value
    to
    .List(.ListCount - 1, 1) = wsMain.Cells(lRw, iX).Text
    to use the value as it appears on the sheet.

  28. #28
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    Thank you so much thats worked great. Ill work on the printing bit to format it into a neater way but Thank you again for all your help. I've learned a lot.

  29. #29
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Loop through names of buttons on a userform

    glad to help :-)

  30. #30
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Loop through names of buttons on a userform

    I really appreciate it thanks

+ 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.6.0 RC 1