0
我最近創建了一個工作簿,其中包含帶有組合框的用戶窗體,名稱爲「combobox1」 我有一個代碼,它將範圍爲「B2:B .. ..「 現在我想讓它如何在一個組合框中的另一列shuld帶來來自同一目錄的數據,但範圍爲exp:」A1:A ....「 我需要你的幫助 thx 。vba使用外部數據的兩列組合框
[Private Sub UserForm_Initialize()
`Dim ListItems As Variant, i As Integer
`Dim SourceWB As Workbook
With Me.ComboBox1
.Clear ' remove existing entries from the listbox
' turn screen updating off,
' prevent the user from seeing the source workbook being opened
Application.ScreenUpdating = False
' open the source workbook as ReadOnly
Set SourceWB = Workbooks.Open("C:\Users\Mohsen\Desktop\new prj\Data base\partlist.xls", _
False, True)
ListItems = SourceWB.Worksheets(1).Range("B2:B1468").Value
' get the values you want
SourceWB.Close False ' close the source workbook without saving changes
Set SourceWB = Nothing
Application.ScreenUpdating = True
ListItems = Application.WorksheetFunction.Transpose(ListItems)
' convert values to a vertical array
For i = 1 To UBound(ListItems)
.AddItem ListItems(i) ' populate the listbox
Next i
.ListIndex = -1 ' no items selected, set to 0 to select the first item
End With
End Sub
非常感謝你的回答,非常有用。我已經將combobox值鏈接到了範圍「C9」,並且它將左列值返回給C9,並且它的OK,現在我想將第二列(右側)鏈接到exp「B9」的單元格,並且我想要用於設置兩個鏈接值的VBA代碼。 –