簡體   English   中英

使用VBA在Excel用戶窗體中選擇由選項指示的多個下拉框

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

這個問題是我正在研究的項目的“第3部分”。 使用vba在運行時將多個標簽和文本框添加到Excel用戶窗體,使用vba 檢索在運行時在Excel用戶窗體中創建的多個文本框的數據之后 ,我現在嘗試使用所有這些數據在下拉框中選擇名稱以進行分配努力。

在此處輸入圖片說明

我遇到的問題是,我已經設置了從LBoundUBound MyArray(i)循環代碼,該代碼為我們提供了雇員的姓名,這樣做,它還循環了通過拆分MultFLNAmt創建的數組從UserForm中檢索到的信息,這樣我們可以確定每個員工將收到多少FLN,然后它還會循環查找找到要分配給的當前員工的姓名。 一旦完成所有這些操作,並且每個人都分配了正確數量的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

雖然代碼在大多數情況下都有效,但失敗的地方是,如果MFA(a)為2或更大,則僅選擇第一個下拉列表。 我將代碼置於調試模式,但沒有看到為什么未選擇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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM