简体   繁体   English

如何动态获取用户窗体控件名称的数字部分-VBA

[英]How to dynamically obtain numeric part of userform control name - VBA

I have 2 small lists of combo boxes. 我有2个组合框小清单。 The first list is labled Type(1-5). 第一个列表标记为Type(1-5)。 The second list is labled Products(1-5). 第二个列表是带有标签的产品(1-5)。 I want to populate each PRODUCT box pending on the selection made in the corresponding TYPE box. 我想填充每个PRODUCT框,这些框在相应的TYPE框中进行选择。 I am currently doing the following... 我目前正在做以下事情...

Private Sub Type1_Change()
    NavComboPropChange
End Sub

Sub NavComboPropChange()
    If BaseActiveControl.Name = "AVM" Then
         = Worksheets("Setup").Range("AVM").Value
    ElseIf BaseActiveControl.Name = "Appraisal" Then
         = Worksheets("Setup").Range("APPRAISAL").Value
    Else
         = Worksheets("Setup").Range("TITLES").Value
    End If
End Sub

BaseActiveControl.name grabs the root control element currently selected. BaseActiveControl.name获取当前选定的根控制元素。 Before the equal sign in the IF, ElseIf, Else sequence would be the product name and the corresponding value. 在IF中的等号之前,ElseIf,Else序列将是乘积名称和相应的值。

To restate my question though, I want to know how I can grab the numeric part of the control name to use in conjunction with the product box name. 不过,要重申我的问题,我想知道如何获取控件名称的数字部分以与产品包装盒名称一起使用。

I found the solution using this route. 我使用此路由找到了解决方案。

Sub NavComboPropChange()
    Dim myString As String
    myString = Right(BaseActiveControl.Name, Len(BaseActiveControl.Name) - 4)
    If BaseActiveControl.Value = "AVM" Then
        Controls("Products" & myString).List = Worksheets("Setup").Range("AVM").Value
    ElseIf BaseActiveControl.Value = "Appraisal" Then
        Controls("Products" & myString).List = Worksheets("Setup").Range("APPRAISAL").Value
    Else
        Controls("Products" & myString).List = Worksheets("Setup").Range("TITLES").Value
    End If
End Sub

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

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