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
I do not know how it would loop through the controls with the different numbers if that is possible?for each control in form Control & 1.caption next
Last edited by djblois1; 06-30-2011 at 03:39 PM.
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
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
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 Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
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.
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 Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Why not using the simplest methods ?
orfor 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
orfor 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
orsn=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
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
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)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks