用于在不同工作簿中选择范围的 VBA 对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18125650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Dialog box to select range in different workbook
提问by dra_red
I want to allow a user to select a range that is likely to be in a different workbook.
我希望允许用户选择可能位于不同工作簿中的范围。
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.
我试图用 inputbox("",type:=8) 来做到这一点,它可以在工作簿中选择数据,但拒绝让我在不同的工作簿中选择一个范围。
Hence I would like a dialog box that allows me to perform this task.
因此,我想要一个允许我执行此任务的对话框。
回答by Siddharth Rout
Since I was free, I created an example for you
由于我有空,我为你创建了一个例子
Create a Userform
and place a ComboBox
, A RefEdit
Control and a Label
创建一个Userform
并放置一个ComboBox
,一个RefEdit
控件和一个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
这是运行用户表单时得到的结果