VBA对话框,用于选择不同工作簿中的范围

dra*_*red 9 excel vba dialog range excel-vba

我想允许用户选择可能在不同工作簿中的范围.

我试图用inputbox("",type:= 8)来做这个,它可以在工作簿中选择数据,但拒绝允许我在不同的工作簿中选择一个范围.

因此,我想要一个允许我执行此任务的对话框.

Sid*_*out 22

由于我是自由的,我为你创造了一个例子

创建一个Userform并放置一个ComboBox,一个RefEdit控件和一个Label

在此输入图像描述

接下来将此代码粘贴到Userform中

Private Sub UserForm_Initialize()
    Dim wb As Workbook

    '~~> Get the name of all the workbooks in the combobox
    For Each wb In Application.Workbooks
        ComboBox1.AddItem wb.Name
    Next

    ComboBox1 = ActiveWorkbook.Name
End Sub

'~~> This lets you toggle between all open workbooks
Private Sub Combobox1_Change()
    If ComboBox1 <> "" Then Application.Workbooks(ComboBox1.Text).Activate

    Label1.Caption = "": RefEdit1 = ""
End Sub

'~~> And this lets you choose the relevant range
Private Sub RefEdit1_Change()
    Label1.Caption = ""

    If RefEdit1.Value <> "" Then _
    Label1.Caption = "[" & ComboBox1 & "]" & RefEdit1
End Sub
Run Code Online (Sandbox Code Playgroud)

这是您运行Userform时获得的

在此输入图像描述


在此输入图像描述


在此输入图像描述

  • 好的,Siddharth. (2认同)