vba 使用 Target 识别单元格位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19900537/
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
Using Target to Recognize a Cell Location
提问by Chris
I run into this issue a fair amount and am curious if someone can tell me why or how I can write this a little cleaner.
我经常遇到这个问题,并且很好奇是否有人能告诉我为什么或如何将这个写得更简洁。
Below is my code and it does work.
下面是我的代码,它确实有效。
If Target.Row = rTime.Offset(0, 1).Row Then
If Target.Column = rTime.Offset(0, 1).Column Then
cboStatus.Activate
End If
End If
How come I can't just write it like this?
我怎么就不能这么写呢?
If Target = rTime.Offset(0, 1) Then
cboStatus.Activate
End If
If target is already a range then why do I need to specify the individual row and individual column? That second code will not work and I have tried many variations of it. I even tried something like If Target.Range = range(“C4”) Then
or If Target.Range = cells(4, 3) Then
, but neither of those worked either. I tried many variations of similar stuff. Although, I don't want to use a specific range like A4
, since I wanted to use the rTime
like what is in the example, but I was just trying to figure this out.
如果目标已经是一个范围,那么为什么我需要指定单个行和单个列?第二个代码不起作用,我尝试了它的许多变体。我什至尝试过类似If Target.Range = range(“C4”) Then
或 的东西If Target.Range = cells(4, 3) Then
,但这些都没有奏效。我尝试了许多类似东西的变体。虽然,我不想使用特定范围,例如A4
,因为我想使用rTime
示例中的内容,但我只是想弄清楚这一点。
Nothing seems to work, other than specifying the individual row and column each time. Can someone please explain this to me? Also, is there a better way to write this than what I did in the first example, which does work?
除了每次指定单独的行和列之外,似乎没有任何效果。有人可以向我解释一下吗?另外,有没有比我在第一个例子中所做的更好的方法来写这个,哪个有效?
Thanks for anything that relieves my confusion.
感谢您所做的一切可以缓解我的困惑。
回答by Siddharth Rout
The default property of a range object is .Value
so when you say If Target = rTime.Offset(0, 1)
, it will always compare the values in that range rather than the address of those ranges.
范围对象的默认属性是.Value
这样,当您说 时If Target = rTime.Offset(0, 1)
,它将始终比较该范围内的值而不是这些范围的地址。
One way is already shown by L42. Here is another way using Intersect
L42 已经展示了一种方式。这是另一种使用方式Intersect
If Not Intersect(Target, rtime.Offset(0, 1)) Is Nothing Then cboStatus.Activate
If Not Intersect(Target, rtime.Offset(0, 1)) Is Nothing Then cboStatus.Activate
EDIT
编辑
When you say Target.Column
and Target.Row
, you will always get the first column and the first row of the cell in that range, even if Target
has multiple cells. To avoid this use the below to ensure that you have the desired Target
. Your code will give you unexpected results even if there is a single cell in Target
. For example, say the value of cell B1
is equal to any other cell which at the moment is target
. So if Cell B1
= "Sid" and Cell F1
= "Sid" and you select cell F1
then you will get the "Hello World" message box.
当您说Target.Column
and 时Target.Row
,即使Target
有多个单元格,您也将始终获得该范围内单元格的第一列和第一行。为避免这种情况,请使用以下内容以确保您拥有所需的Target
. 即使Target
. 例如,假设单元格的值B1
等于此时为 的任何其他单元格target
。因此,如果 Cell B1
= "Sid" 和 Cell F1
= "Sid" 并且您选择单元格,F1
那么您将收到“Hello World”消息框。
For xl2003, you can use an additional check
对于 xl2003,您可以使用额外的检查
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rtime As Range
Set rtime = Range("A1")
If Target.Cells.Count > 1 Then
MsgBox "you have chosen more than one cell"
Exit Sub
End If
If Not Intersect(Target, rtime.Offset(0, 1)) Is Nothing Then
MsgBox "Hello World"
End If
End Sub
For xl2007+, replace Target.Cells.Count
with Target.Cells.CountLarge
对于 xl2007+,替换Target.Cells.Count
为Target.Cells.CountLarge
For L42
对于 L42
Your method is correct but then you also will have to put the above check to get the correct results.
您的方法是正确的,但是您还必须进行上述检查才能获得正确的结果。
回答by L42
try this:
尝试这个:
Edit1:To cover Chis' concern
Edit1:为了掩盖Chis的担忧
If Target.Address = rtime.Offset(0,1).Address(,,,True) then cboStatus.Activate
you cannot compare objects, just the properties? I'm not certain though.
你不能比较对象,只能比较属性?不过我不确定。