vba 单击单元格时显示用户表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18892664/
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
Bring up user form when clicking on cell
提问by apkdsmith
I have a spreadsheet with a list of people in the first column and various bits of information about them in the rest of each row. I have a userform which will populate and subsequently edit each record, which I want to have pop up when the user clicks on the person's name.
我有一个电子表格,第一列中有一个人员列表,每行的其余部分都有关于他们的各种信息。我有一个用户表单,它将填充并随后编辑每条记录,我希望在用户单击此人的姓名时弹出这些记录。
e.g.:
例如:
Name Age DOB Postcode
John Smith 55 3/6/1958 RM3 5WO
Mary Jones 22 4/2/1991 RM2 6TP
So I want to be able to click on John Smith (A2) to bring up a form with fields for his age, DOB, postcode etc. I also want to be able to use the form to add entries, so it will also function on at least one row below the list.
所以我希望能够点击 John Smith (A2) 以显示一个包含他的年龄、出生日期、邮政编码等字段的表单。我还希望能够使用该表单添加条目,因此它也可以在列表下方至少一行。
I've been given the following code to use:
我得到了以下代码来使用:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A" Then
frm_tch_staffinfo.Show
End If
End Sub
I've tried using this in the sheet, but it doesn't seem to be working - nothing happens when I click on A2. Is there some error in the code or how I've implemented it? (I've put it in the code for the relevant sheet).
我试过在工作表中使用它,但它似乎不起作用 - 单击 A2 时没有任何反应。代码中是否有错误或我是如何实现的?(我已将其放入相关工作表的代码中)。
Additionally, I want to expand the target address to include the range of all the names (the number of which may change) and at least one cell below (but not the header in A1). Is it possible to define such a range? Either simply selecting A2 and below, or using a process which counts the cells which are full and adds on or more at the end.
此外,我想扩展目标地址以包括所有名称的范围(其数量可能会更改)和下面至少一个单元格(但不是 A1 中的标题)。是否可以定义这样的范围?要么简单地选择 A2 及以下,要么使用一个过程来计算已满的单元格并在最后添加或更多。
Thanks for your help!
谢谢你的帮助!
ETA:
I definitely require Excel for this, as my usage is that of a spreadsheet rather than a database. This was a simplified example: the data is in fact all numeric and used in relatively complex calculations in other worksheets.
ETA:
我绝对需要 Excel,因为我使用的是电子表格而不是数据库。这是一个简化的示例:数据实际上都是数字,并用于其他工作表中相对复杂的计算。
采纳答案by Trace
As mentioned in comment: the first part of your code works just fine (selection change).
I just did the test and there is nothing wrong with it.
Just note:
如评论中所述:您的代码的第一部分工作正常(选择更改)。
我刚刚做了测试,没有任何问题。
请注意:
- Put the correct form name;
- Place the code in the correct sheet module
- 输入正确的表格名称;
- 将代码放在正确的工作表模块中
As for your second question, the code you are looking for:
至于你的第二个问题,你要找的代码:
Set oRange = Range("A1:A10")
If Not Intersect(Target, oRange) Is Nothing Then
'do something
End If
If you want to set the range dynamically, there have been many posts on this before.
Check out for example:
Getting the actual usedrange
如果你想动态设置范围,之前已经有很多帖子了。
查看例如:
获取实际使用的范围