从另一个工作簿填充组合框

时间:2020-07-22 11:17:40

标签: excel vba

假设我的数据在名为“ affectation”的工作表中并且数据在第一列中,我如何从另一个工作簿中填充一个组合框 enter image description here 我的组合框处于用户窗体中,可使用以下代码从现成的工作簿中填充它:

do.call(rbind, lapply(levs, my_iris_data, data = iris))

我想从另一个工作簿中填充它。

1 个答案:

答案 0 :(得分:0)

我(仅)假设您需要使用另一个工作簿中的数据填充组合框。如果我的理解正确,请尝试以下代码:

Private Sub CommandButton1_Click__()
 Dim ws_Liste_affect As Worksheet, Fin_Liste_affect As Long, arr As Variant
 Dim wbFullPath As String, wb As Workbook, boolFound As Boolean
 
 wbFullPath = "C:\...\TheOtherWorkbook.xls"
 For Each wb In Workbooks
    If wb.FullName = wbfullname Then
        Set ws_Liste_affect = wb.Worksheets("the other sheet")
        boolFound = True: Exit For
    End If
 Next
 If Not boolFound Then
    Set wb = Workbooks.Open(vbfullpath)
    Set ws_Liste_affect = wb.Worksheets("the other sheet")
 End If
 
 Fin_Liste_affect = ws_Liste_affect.Range("A" & Rows.count).End(xlUp).Row

 arr = ws_liste.affect.Range("A2:A" & Fin_Liste_affect).Value
 UserForm1.ComboBox_affect.List = arr

 UserForm1.Show
End Sub