VBA对话框选择不同工作簿中的范围 [英] VBA Dialog box to select range in different workbook
问题描述
我希望允许用户选择可能位于不同工作簿中的范围.
I want to allow a user to select a range that is likely to be in a different workbook.
我尝试使用 inputbox("",type:=8) 来执行此操作,它可以在工作簿中选择数据,但拒绝让我在不同的工作簿中选择范围.
I have attempted to do this with inputbox("",type:=8) which works to select data in the workbook but refuses to allow me to select a range in a different workbook.
因此,我想要一个允许我执行此任务的对话框.
Hence I would like a dialog box that allows me to perform this task.
推荐答案
既然有空,就给大家做个例子
Since I was free, I created an example for you
创建一个 Userform
并放置一个 ComboBox
、一个 RefEdit
控件和一个 Label
Create a Userform
and place a ComboBox
, A RefEdit
Control and a Label
接下来将这段代码粘贴到用户表单中
Next paste this code in the 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
这是您运行用户表单时得到的结果
This is what you get when you run the Userform
这篇关于VBA对话框选择不同工作簿中的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!