Private Sub Command1_Click() Dim c As Control For Each c In Me.Controls Debug.Print c.Name Next End Sub上面的代码是显示窗体中所有控件的名称,稍加修改即可完成楼主的要求。
你是说取得同一类型控件的数字最大的控件的名称还是当前窗体所有的阿,如果同一种直接去index就可以了。如果当前窗体可以Private Sub Command1_Click() Dim c As Control Dim Num As Integer Dim strName As String Dim i As Integer Dim blnFirst As Boolean '是否第一个控件 blnFirst = True For Each c In Me.Controls If blnFirst Then strName = c.Name Num = -1 blnFirst = False End If For i = 1 To Len(c.Name) If Not IsNumeric(Right(c.Name, i)) Then Exit For End If Next If Val(Right(c.Name, i - 1)) > Num Then Num = Right(c.Name, i - 1) strName = c.Name End If Next
MsgBox "当前控件名称中数字最大的控件的名称" & strName & ",数字为" & Num End Sub
Dim c As Control
For Each c In Me.Controls
Debug.Print c.Name
Next
End Sub上面的代码是显示窗体中所有控件的名称,稍加修改即可完成楼主的要求。
Dim Num As Integer
Dim strName As String
Dim i As Integer
Dim blnFirst As Boolean '是否第一个控件
blnFirst = True
For Each c In Me.Controls
If blnFirst Then
strName = c.Name
Num = -1
blnFirst = False
End If
For i = 1 To Len(c.Name)
If Not IsNumeric(Right(c.Name, i)) Then
Exit For
End If
Next
If Val(Right(c.Name, i - 1)) > Num Then
Num = Right(c.Name, i - 1)
strName = c.Name
End If
Next
MsgBox "当前控件名称中数字最大的控件的名称" & strName & ",数字为" & Num
End Sub