使用VBAselect由Excel用户窗体中的选项指定的多个下拉框

这个问题是我正在进行的项目的“第三部分”。 在使用vba在运行期间将多个标签和文本框添加到Excel用户窗体并使用vba 从Excel用户窗体运行时创build的多个文本框中检索数据之后 ,我现在试图使用所有数据来select要分配的下拉框中的名称工作。

在这里输入图像说明

我遇到的问题是,我有代码设置循环MyArray(i)LBoundUBound ,给我们员工的名字,因为它这样做,它也循环通过分裂MultFLNAmt创build一个数组这是从UserForm中检索的,因此我们可以确定每个员工将接收多less个FLN,然后循环查找select分配给的当前员工的名称。 一旦完成了这一切,每个人都有正确数量的FLN分配,它将单击应用程序中的提交button完成作业。

 ' 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 

尽pipe代码大部分工作在失败的地方,但如果MFA(a)是2或更多,则只select第一个下拉列表。 我把代码放在debugging模式下,我不明白为什么2个或更多没有被选中。 有任何想法吗?

经过大量的研究,我终于想出了如何让我的项目工作。

 ' 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