简体   繁体   English

使用VBA在Excel用户窗体中选择由选项指示的多个下拉框

[英]Selecting multiple dropdown boxes indicated by options in an Excel UserForm using VBA

This question is a "part 3" of a project I'm working on. 这个问题是我正在研究的项目的“第3部分”。 After adding multiple labels and textboxes to an Excel userform during runtime using vba and retrieving data from multiple textboxes created during runtime in an Excel userform using vba , I'm now trying to use all that data to select the names in the dropdown boxes to assign work to. 使用vba在运行时将多个标签和文本框添加到Excel用户窗体,使用vba 检索在运行时在Excel用户窗体中创建的多个文本框的数据之后 ,我现在尝试使用所有这些数据在下拉框中选择名称以进行分配努力。

在此处输入图片说明

The issue I'm having is, I have the code set up to loop through MyArray(i) from LBound to UBound that gives us the names of the employees, as it does this, it is also looping through an array created by splitting MultFLNAmt that was retrieved from the UserForm so we can determine how many FLNs each employee will receive, then it also loops through to find the name of the current employee selected to assign to. 我遇到的问题是,我已经设置了从LBoundUBound MyArray(i)循环代码,该代码为我们提供了雇员的姓名,这样做,它还循环了通过拆分MultFLNAmt创建的数组从UserForm中检索到的信息,这样我们可以确定每个员工将收到多少FLN,然后它还会循环查找找到要分配给的当前员工的姓名。 Once all of this is done and everyone has the correct amount of FLNs assigned, it will click the Submit button in the application to finish the assignment. 一旦完成所有这些操作,并且每个人都分配了正确数量的FLN,它将单击应用程序中的“提交”按钮以完成分配。

' Shows and hides the multiple option UserForm
MultipleOptionForm.Show

MultipleOptionForm.Hide

' Creates an array from a comma-delimited
' list of numbers stored in a variable
MFA = Split(MultFLNAmt, ",")

' Activates the application we will be assigning work from
WShell.AppActivate "Non-Keyable Document Management System"

' Table cell node where the dropdown is located
tdNode = 64
a = 1

' Loop through each of the names within the array
For c = LBound(MyArray) + 1 To UBound(MyArray) - 1
    ' Loop through the array to see how many FLNs each person receives
    For b = 1 To MFA(a)
        ' Loop through to locate the current name of the employee
        i = 0
        For Each objOption In objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).Options
            Q(i) = objOption.Text & "-" & objOption.Value

            strWQ = Q(i)
            ' Remove "Selected User" from the list of options
            If i = 0 Then
                If strWQ = "--Select User---" Then strWQ = ""
            Else
                ' If an option matches the current name selected,
                ' select that option, then increase the node location
                ' for the next dropdown box
                If InStr(strWQ, MyArray(c)) Then
                    objOption.Selected = True
                    objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).OnChange
                    tdNode = tdNode + 23
                Else
                    objOption.Selected = False
                End If
            End If
        Next            
        i = i + 1            
    Next
Next

objIE.Document.all.Item("btn_submit1").Click

While the code is working for the most part, where it's failing is, if MFA(a) is 2 or more, only the first dropdown is selected. 虽然代码在大多数情况下都有效,但失败的地方是,如果MFA(a)为2或更大,则仅选择第一个下拉列表。 I put the code in debugging mode and I'm not seeing why 2 or more are not being selected. 我将代码置于调试模式,但没有看到为什么未选择2个或更多的代码。 Any ideas? 有任何想法吗?

After a lot of research, I finally figured out how to get my project to work. 经过大量研究,我终于想出了如何使我的项目生效。

' This line allows for growth/shrinkage of the list of employees
MultipleOptionForm.Height = (UBound(MyArray) - 1) * 20

' This line shows the form
MultipleOptionForm.Show

' This line hides the form after being updated
MultipleOptionForm.Hide

' Creates an array from a comma-delimited
' list of numbers stored in a variable
MFA = Split(MultFLNAmt, ",")

' Activates the application we will be assigning work from
WShell.AppActivate "Non-Keyable Document Management System"

' Table cell node where the dropdown is located
tdNode = 64
' MFA index
a = 1

' Loop through each of the names within the array
For c = LBound(MyArray) + 1 To UBound(MyArray) - 1
    ' Loop through the array to see how many FLNs each person receives
    For b = 1 To MFA(a)
        ' Starts loop at first drop down
        On Error Resume Next
            For Each objOption In objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).Options
                ' Stores options within drop down
                strWQ = objOption.Text & "-" & objOption.Value
                If IsEmpty(strWQ) Then
                    Exit Sub
                End If
                ' Remove "Selected User" from the list of options
                If strWQ = "--Select User---" Then
                    strWQ = ""
                Else
                    ' If there's a match between the drop down for the list
                    ' and the list of assigned FLNs, begin assigning
                    If InStr(strWQ, MyArray(c)) Then
                        objOption.Selected = True
                        objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).OnChange
                        tdNode = tdNode + 23
                        Exit For
                    Else
                        objOption.Selected = False
                    End If
                End If
            Next
        On Error GoTo 0
    Next
Next

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

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