VBA对话框在不同的工作簿中select范围

我想让用户select一个可能在不同工作簿中的范围。

我试图用inputbox(“”,type:= 8)来做到这一点,它可以在工作簿中select数据,但拒绝让我在不同的工作簿中select一个范围。

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

自从我有空,我为你创造了一个榜样

创build一个用户Userform并放置一个ComboBox ,一个RefEdit控件和一个Label

在这里输入图像说明

接下来将此代码粘贴到用户窗体中

 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 

这是您运行用户窗体时得到的

在这里输入图像说明


在这里输入图像说明


在这里输入图像说明