excel vba表单变量下拉列表

jxct1oxe  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(169)

我在Excel VBA中创建了一个表单,并希望创建一个带有变量输入的下拉列表。目前,这是设置为Site1,Site2和Site3,但我想检索这从工作表“来源”在范围A:A。该列表将来可能会增长,因此希望它考虑A:A中的所有输入(无论它有2个值还是10个值,它都应该考虑所有值)。
在module1中,我有下面的代码:

Sub EnterPurchaseOrder()

...
        
        Call ShowUserForm

...    

End Sub
Sub ShowUserForm()
    Dim myForm As New createPurchaseOrder
    myForm.Show
End Sub
function findQuantity() as double

Application.ScreenUpdating = False

'Declare variables
Dim quantity As Double

quantity = ActiveCell.Value

findQuantity = quantity

Application.ScreenUpdating = True

End Sub

我还有一个名为“createPurchaseOrder”的表单,代码如下:

Sub UserForm_Initialize()
    
Dim orderQty As Double

orderQty = findQuantity

    purchOrd.Value = ""
    
    sourceDropDown.Clear
    With sourceDropDown
        .AddItem "Site1"
        .AddItem "Site2"
        .AddItem "Site3"
    End With
    
    mmCode = ""
    
    orderQuantity = orderQty
    
    releaseDate = ""
    
    purchOrd.SetFocus

End Sub

有人能帮我这个忙吗?因为我一直在努力让这个工作,但没有取得进展......
非常感谢。
亲切问候Jan

nr7wwzry

nr7wwzry1#

而不是这个

With sourceDropDown
        .AddItem "Site1"
        .AddItem "Site2"
        .AddItem "Site3"
    End With

试试这个

Dim wsSource As Worksheet
  Set wsSource = ThisWorkbook.Worksheets("Sources")

  With wsSource
    Dim LastRow As Long
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  End With

  sourceDropDown.List = wsSource.Range("A1:A" & LastRow).Value

相关问题