从Userform变量

时间:2017-03-14 10:46:19

标签: vba excel-vba excel

我在Userform中有两个简单的代码片段以及我想要做的事情,我相信这很简单,但是我被卡住了!

Private Sub UserForm_Initialize()

Dim wkb As Workbook

    With Me.CB_Excel_File
        For Each wkb In Application.Workbooks
            .AddItem wkb.Name
        Next wkb
    End With

End Sub

Private Sub CB_Excel_File_Change()

Dim wks As Worksheet

    With Me.CB_Worksheet
        For Each wks In ***Me.CB_Excel_File.Value.Worksheets***
            .AddItem wks.Name
        Next wks
    End With

End Sub

我想要列出用户从Userform_Initialize代码中选择的工作簿中的所有工作表。

提前致谢!

1 个答案:

答案 0 :(得分:2)

您需要将所选工作簿名称作为变量传递到Workbooks集合中,如下所示:

Private Sub CB_Excel_File_Change()

Dim wks As Worksheet

With Me.CB_Worksheet
    For Each wks In Workbooks(CB_Excel_File.Value).Worksheets
        .AddItem wks.Name
    Next wks
End With

End Sub