Results 1 to 6 of 6

Thread: [RESOLVED] [Excel] How do I go through all controls on a userform

Hybrid View

  1. #1

    Thread Starter
    Lively Member adifel's Avatar
    Join Date
    Jan 2006
    Location
    California
    Posts
    103

    Resolved [RESOLVED] [Excel] How do I go through all controls on a userform

    Hi,

    I have this userform in VBA Excel 2010, which has around 300 controls, most majority being checkboxes, the rest are option buttons, labels and textboxes; however, I need to save the status for each and all of them (value for checkboxes and option buttons, text and caption for textboxes and labels), so it can be recovered at a later time.

    I thought to do this with a txt file with each line having a stat for one of the controls.

    Question is, is there an easy way to go through all the controls, without checking them one by one? Or is there an better way to save them?

    Thanks.

  2. #2
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: [Excel] How do I go through all controls on a userform

    you will have to do a for each control in controls type loop and collect each and every setting that you think is important and you will need to interogate the controls as you find them to know the property or properties that are appropriate to each type..

    the advantage to this process it that the controls will be collected in order and the data will be able to be read back in order too...

    unless of course you store the control enumeration as well as the properties, then you can fill in the controls in any order!

    here to help

  3. #3

    Thread Starter
    Lively Member adifel's Avatar
    Join Date
    Jan 2006
    Location
    California
    Posts
    103

    Re: [Excel] How do I go through all controls on a userform

    Thanks for the response. As I'm kinda new to VBA, can you give an example of a loop? How do I go through all the controls of one type (e.g. all textboxes), given that they all have different names?

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

    Re: [Excel] How do I go through all controls on a userform

    can you give an example of a loop?
    vb Code:
    1. for each c in me.controls
    2.     'for most controls you will want to save the default value against the name value
    3.    print #f, c.name & vbtab & c.value
    4. next
    where f is the number of a file opened for output or append

    if you have to do them in some order it is more difficult as you probably have to loop separately for each type of control

    if you just want to use to repopulate the form at later time the order does not matter
    read the text file into an array and for each line set the value of the control like
    vb Code:
    1. for i = 0 to ubound(filearray)
    2.    tmp = split(filearray(i), vbtab)
    3.    me.controls(tmp(0)).value = tmp(1)
    4. next
    where filearray is an array filled by splitting the lines from the textfile
    Last edited by westconn1; Feb 15th, 2012 at 04:43 AM.
    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

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    21

    Re: [RESOLVED] [Excel] How do I go through all controls on a userform

    Do it like this

    Code:
    Private Sub CommandButton1_Click()
        
        Dim Counter As Integer
            
        For Each Ctrl In Me.Controls
            If TypeName(Ctrl) = "TextBox" Then
                Counter = Counter + 1
                Sheets("Sheet1").Range("A" & Counter).Value = Ctrl.Name
                Sheets("Sheet1").Range("B" & Counter).Value = Ctrl.Text
            End If
        Next
    End Sub

  6. #6

    Thread Starter
    Lively Member adifel's Avatar
    Join Date
    Jan 2006
    Location
    California
    Posts
    103

    Re: [Excel] How do I go through all controls on a userform

    I will try that. Thank you!

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