简体   繁体   English

使用变量定义控件在 vba 中的表单上定义控件标题

[英]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.我有一个用户表单,SortForm,上面有 30 个单选按钮。 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 和 intSortCol。 strSortName contains the course name. strSortName 包含课程名称。 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每个单选按钮名称都以“btSort”开头,然后是 1 到 30 的数字。我想要做的是使用“btsort”和我的 do 循环中的索引的连接来更新按钮的标题以反映课程名称包含在 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 ' Sub macSortData() ' ' macSortData 宏 ' ' 键盘快捷键: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 Dim strVal1 As String, intVal2 As Integer, I As Integer Dim FormName As String ' 不确定是否需要 Dim ControlName As Controls ' 不确定这是否需要或正确 Dim intSortCol(34), strSortName(34) As String

Sheets("Main").Activate工作表(“主要”)。激活

For I = 1 To 34对于 I = 1 到 34

''strVal1 contains the course name and strVal2 contains the column number for the course ''strVal1 包含课程名称,strVal2 包含课程的列号

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 strSortName(I) = strVal1 intSortCol(I) = intVal2

'non working caption update code Set ControlName = "btsort" & Trim(str(I)) ' concatenated control name '非工作字幕更新代码Set ControlName = "btsort" & Trim(str(I)) ' 连接的控件名称

SortForm.ControlName.Caption = strSortName(I) 'non-working assignment attempt SortForm.ControlName.Caption = strSortName(I) '非工作分配尝试

Next I接下来我

sortFinal:排序最终:

'Display form SortForm.Show '显示表格SortForm.Show

End Sub结束子

The code that worked is有效的代码是

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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