+ Reply to Thread
Results 1 to 8 of 8

Thread: Add Caption to Options Buttons in Order

  1. #1
    Forum Contributor
    Join Date
    08-23-2010
    Location
    Staten Island, NY
    MS-Off Ver
    Excel 2003
    Posts
    180

    Add Caption to Options Buttons in Order

    Say I have 10 Option Buttons on a form that I call .ob1, .ob2, .ob3, up to .ob10

    Then say the captions on these buttons need to change based on code. Is there a way to loop through and change the captions with an array or do I need to change each caption individually?

    something like

    
    for each control in form
             Control & 1.caption
    next
    I do not know how it would loop through the controls with the different numbers if that is possible?
    Last edited by djblois1; 06-30-2011 at 03:39 PM.

  2. #2
    Valued Forum Contributor tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    USA
    MS-Off Ver
    Excel 2003 - 2007
    Posts
    2,352

    Re: Add Caption to Options Buttons in Order

    djblois1,

    You can use the following to loop through each option button on a userform:
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "OptionButton" Then
            MsgBox "Option Button Name: " & ctrl.Name & Chr(10) & "Caption: " & ctrl.Caption
        End If
    Next ctrl


    Hope that helps,
    ~tigeravatar

  3. #3
    Forum Contributor
    Join Date
    08-23-2010
    Location
    Staten Island, NY
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Add Caption to Options Buttons in Order

    This is what I do now:

    With Me
                .obSelect1.Caption = "Product Description"
                .obSelect2.Caption = "Division"
                .obSelect3.Caption = "Deptartment#"
                .obSelect4.Caption = "Department Name"
                .obSelect5.Caption = "Product Category"
                .obSelect6.Caption = "Product Manager"
                .obSelect7.Caption = "Second Manager"
                .obSelect8.Caption = "Country"
                .obSelect9.Caption = "Weight"
                .obSelect10.Caption = "UPC"
                .Caption = "Add Product Info"
            End With

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Add Caption to Options Buttons in Order

    Hello djblois1,

    It is easier to used a named range to hold the list of captions. In this macro a named range "Captions" is used. The macro retrieves the sequence number of the Option Button from its name and uses it as index into the named range. The allows the option Buttons to be retrieved in any order and still have the correct caption assigned.
    Sub AddCaptions()
    
      Dim Ctrl As Object
      Dim I As Integer
      Dim RegExp As Object
      
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.Pattern = "\D+(\d+)"
        
        For Each Ctrl In Me.Controls
          If TypeName(Ctrl) = "Option Button" Then
            If Ctrl.Name Like "obSelect*" Then
               I = RegExp.Replace(Ctrl.Name, "$1")
               Ctrl.Caption = Range("Captions").Item(I)
            End If
          End If
        Next Ctrl
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Contributor
    Join Date
    08-23-2010
    Location
    Staten Island, NY
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Add Caption to Options Buttons in Order

    I do not understand why it is using a vbscript in that code?

    Plus, I cannot use a named range. I want to use an Array if possible.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Add Caption to Options Buttons in Order

    Hello djblois1,

    Here is the revised macro using a fixed array. The Regular Expressioin (RegExp) is used to parse the sequence number from of the Option Button's name. This sequence number is used as the index for the Caption array.
    Sub AddCaptions()
    
     'Array version
     
      Dim Captions As Variant
      Dim Ctrl As Object
      Dim I As Integer
      Dim RegExp As Object
      
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.Pattern = "\D+(\d+)"
        
        ReDim Captions(1 To 10)
          Captions(1) = "Product Description"
          Captions(2) = "Division"
          Captions(3) = "Deptartment#"
          Captions(4) = "Department Name"
          Captions(5) = "Product Category"
          Captions(6) = "Product Manager"
          Captions(7) = "Second Manager"
          Captions(8) = "Country"
          Captions(9) = "Weight"
          Captions(10) = "UPC"
        
        For Each Ctrl In Me.Controls
          If TypeName(Ctrl) = "Option Button" Then
            If Ctrl.Name Like "obSelect*" Then
               I = RegExp.Replace(Ctrl.Name, "$1")
               Ctrl.Caption = Captions(I)
            End If
          End If
        Next Ctrl
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Add Caption to Options Buttons in Order

    Why not using the simplest methods ?

    for j=1 to 10
      Me("ObSelect" & j).caption=choose(j,"Product Description","Division","Deptartment#","Department Name","Product Category", "Product Manager","Second Manager","Country","Weight","UPC")
    next
    or
    for j=1 to 10
      Controls("ObSelect" & j).caption=choose(j,"Product Description","Division","Deptartment#","Department Name","Product Category", "Product Manager","Second Manager","Country","Weight","UPC")
    next
    or
     sn=array("","Product Description","Division","Deptartment#","Department Name","Product Category", "Product Manager","Second Manager","Country","Weight","UPC")
     for j=1 to 10
      Me("ObSelect" & j).caption=sn(j)
     next
    or
     for j=1 to 10
      Me("ObSelect" & j).caption=split("|Product Description|Division|Deptartment#|Department Name|Product Category|Product Manager|Second Manager|Country|Weight|UPC","|")(j)
    next



  8. #8
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Add Caption to Options Buttons in Order

    You could try
    Sub AddCaptions()
    
    Dim iX As Integer
    For iX = 1 To 9
    Me("ob" & iX).Caption = Choose(iX, "Division", "Deptartment#", _
        "Department Name", "Product Category", "Product Manager", "Second Manager", _
        "Country", "Weight", "UPC")
    Next iX
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ 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.2.0