Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 如何在VBA中检索动态创建的组合框的名称?_Forms_Vba_Excel_Combobox - Fatal编程技术网

Forms 如何在VBA中检索动态创建的组合框的名称?

Forms 如何在VBA中检索动态创建的组合框的名称?,forms,vba,excel,combobox,Forms,Vba,Excel,Combobox,我想引用我使用循环创建的组合框的值,但我不知道它们的名称。我怎样才能找到他们的名字 Sub addLabel() Dim theLabel As Object Dim theRanker As Object Dim labelCounter As Long Dim RowCount As Integer RowCount = Sheets("Overview").Range("A" & Rows.count).End(xlUp).Row For labelCounter = 1 To

我想引用我使用循环创建的组合框的值,但我不知道它们的名称。我怎样才能找到他们的名字

Sub addLabel()
Dim theLabel As Object
Dim theRanker As Object
Dim labelCounter As Long
Dim RowCount As Integer

RowCount = Sheets("Overview").Range("A" & Rows.count).End(xlUp).Row

For labelCounter = 1 To RowCount
    Set theRanker = CriteriaPairwiseForm.Controls.Add("Forms.ComboBox.1", "Rating" & labelCounter, True)
    With theRanker
        .Left = 20
        .Width = 150
        .Top = 30 * labelCounter
        .AddItem "Equal Importance"
        .AddItem "Moderate Importance"
        .AddItem "Strong Importance"
        .AddItem "Very Strong Importance"
        .AddItem "Extreme Importance"

    End With
    Set theLabel = CriteriaPairwiseForm.Controls.Add("Forms.Label.1", "CriteriaRank" & labelCounter, True)
    With theLabel
        .caption = Cells(labelCounter, 1).Value
        .Left = 200
        .Width = 150
        .Top = 30 * labelCounter

    End With
Next labelCounter
End Sub

最简单的方法是使用从
.Add
方法获得的引用来命名它们。您已经在循环一系列值,因此请使用循环计数器生成对象名称。这样做的优点是还具有与标签来源行对应的名称:

Sub addLabel()
    Dim theLabel As Object
    Dim theRanker As Object
    Dim labelCounter As Long
    Dim RowCount As Integer

    RowCount = Sheets("Overview").Range("A" & Rows.Count).End(xlUp).Row

    For labelCounter = 1 To RowCount
        Set theRanker = CriteriaPairwiseForm.Controls.Add("Forms.ComboBox.1", _
                        "Rating" & labelCounter, True)
        With theRanker
            .Left = 20
            .Width = 150
            .Top = 30 * labelCounter
            .AddItem "Equal Importance"
            .AddItem "Moderate Importance"
            .AddItem "Strong Importance"
            .AddItem "Very Strong Importance"
            .AddItem "Extreme Importance"
            .Name = "Ranker" & labelCounter
        End With
        Set theLabel = CriteriaPairwiseForm.Controls.Add("Forms.Label.1", _
                       "CriteriaRank" & labelCounter, True)
        With theLabel
            .Caption = Cells(labelCounter, 1).Value
            .Left = 200
            .Width = 150
            .Top = 30 * labelCounter

        End With
    Next labelCounter
End Sub
以后可以使用相同的方法从中获取值(或通过连接名称按行索引对其进行寻址)。您可以通过创建一个名为“CriteriaPairwiseForm”的用户表单,并在表单中添加以下代码以及
子addLabel
,来验证这一点:

Option Explicit

Private Sub UserForm_Initialize()
    addLabel
End Sub

Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    GetValues
End Sub

Private Sub GetValues()

    Dim labelCounter As Long
    Dim RowCount As Integer
    Dim message As String

    RowCount = Sheets("Overview").Range("A" & Rows.Count).End(xlUp).Row

    For labelCounter = 1 To RowCount
        message = "Ranker" & labelCounter & " contains:" & vbCrLf & _
                  CriteriaPairwiseForm.Controls("Ranker" & labelCounter).Text
        MsgBox message
    Next labelCounter

End Sub

你试过使用
theRanker.Name
吗?你在上面的代码中找到了一个引用,为什么不把它们命名为方便的名称呢?Tim,它返回了“Rating3”,但当我试着使用Rating3.value引用它的值时,它说变量没有定义。
theRanker.value
可以做到这一点。如果你真的想通过名字来引用它,那么你可以做
CriteriaPairwiseForm.Controls(theRanker.name).Value
,但这是一种循环占位符,它是一个循环,根据用户输入的变量数量创建不同数量的组合框,所以我想我不能一一列举。我无法从第二个代码中看到任何值。@Lillian-在我测试它时工作得很好。当您尝试读取值时,表单是否仍处于打开状态?是的,表单仍处于打开状态。当我更改组合框的值时,我在即时窗口中看不到任何值。@Lillian-我根本无法复制它,我可能需要查看代码的其余部分。上面我没有包括的代码的唯一部分是
Sub UserForm_Initialize()Sheets(“概述”).Range(“A1”)。选择“将Do循环设置为在到达空单元格时停止”。直到IsEmpty(ActiveCell)addLabel'从当前位置下一行。ActiveCell.Offset(1,0)。选择Loop End Sub
,这样当我运行代码时,组合框就会出现,并且进行选择,但是您提供的代码
CriteriaPairwiseForm.Controls(“Ranker”&labelCounter”)。Text
不会从组合框返回选择,就像我需要它为空一样。