简体   繁体   中英

Define a control caption on a form in vba using a variable to define the control

Please let me start again.

I have a user form,SortForm that has 30 radio buttons on it. The purpose of the form is to allow the user to pick one of the buttons to use as a key to sort the spreadsheet. It is sorting on courses and these change for each registration period.

What I am attempting to do is change the captions of radio buttons to reflect the course names. I have store the information from the spreadsheet into two arrays,strSortName and intSortCol. strSortName contains the course name. Each radio buttons name starts with "btSort" and then a number 1 to 30. What I want to do is using the concatenation of "btsort" and the index from my do loop to update the caption of the button to reflect the course name contain in strSortName

I hope this is a better question

Charley

Code to date , Stuck on updating Captions

Sub macSortData() ' ' macSortData Macro ' ' Keyboard Shortcut: Ctrl+Shift+S '

Dim strVal1 As String, intVal2 As Integer, I As Integer Dim FormName As String ' not sure is needed Dim ControlName As Controls ' not sure if this is needed or correct Dim intSortCol(34), strSortName(34) As String

Sheets("Main").Activate

For I = 1 To 34

''strVal1 contains the course name and strVal2 contains the column number for the course

strVal1 = Cells(4, I + 2).Value
intVal2 = I + 2

'tests for ending list of classes

 If Len(strVal1) = 0 Then GoTo sortFinal
 If strVal1 = "# taken" Then GoTo sortFinal

'stores the name and column number in arrays
strSortName(I) = strVal1 intSortCol(I) = intVal2

'non working caption update code Set ControlName = "btsort" & Trim(str(I)) ' concatenated control name

SortForm.ControlName.Caption = strSortName(I) 'non-working assignment attempt

Next I

sortFinal:

'Display form SortForm.Show

End Sub

The code that worked is

ControlName = "btsort" & Trim(str(I))

SortForm.Controls(ControlName).Caption = Trim(strSortName(I))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM