[英]Macro that looks at the output of the cell rather than the formula
I've used the code from Microsoft's web page so that every time a cell in my range changes, a message box automatically tells me this has happened. 我使用了Microsoft网页上的代码,因此,每次更改范围内的单元格时,都会出现一个消息框,自动告诉我发生了这种情况。 The page is: https://support.microsoft.com/en-us/kb/213612 该页面是: https : //support.microsoft.com/en-us/kb/213612
The code I've copied and pasted is (I've very slightly edited it - the edits are in the code below): 我复制并粘贴的代码是(我对其进行了非常小的编辑-编辑在下面的代码中):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("B1:B10")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
' Display a message when one of the designated cells has been
' changed.
' Place your code here.
MsgBox "Cell " & Target.Address & " has changed."
End If
End Sub
However this code isn't doing exactly what I'd like to do. 但是,这段代码并没有完全按照我的意愿去做。 Inside B10 I have the formula: 在B10内,我有一个公式:
=IF(D10="709A", IF(AND(D10="709A"), "Yes", "No"), "")
The formula itself isn't important, it's what the formula does. 公式本身并不重要,而是公式的作用。 The formula outputs "Yes" in B10 if D10 is input with "709A". 如果用“ 709A”输入D10,则公式在B10中输出“是”。 Therefore, according to my knowledge, I should automatically get a message box from my macro if I input "709A" into D10, because it changes the value of B10. 因此,据我所知,如果我在D10中输入“ 709A”,则应该从宏中自动获得一个消息框,因为它会更改B10的值。 But I don't. 但是我没有。 I only get a message box if the formula itself changes, even if the output (value/what's visible in the cell - is there a technical label for this?) doesn't change. 即使公式本身发生更改,即使输出(值/在单元格中可见的内容-是否为此有技术标签?)不变,我也只会收到一个消息框。 How can I create a macro that automatically tells me when the output of a cell changes? 如何创建一个宏,该宏可以在单元格的输出发生变化时自动告诉我? Ie: that tells me that the output has changed from "" to "Yes" when D10 is input with "709A"? 即:这告诉我当用“ 709A”输入D10时,输出已从“”更改为“是”?
A workaround that I can think of is to have an array of variables in the Worksheet_Change event that would store the .value or .value2 property of each of your key cells, and compare it to the changed cell, and trigger your message box that way. 我可以想到的一种解决方法是在Worksheet_Change事件中具有一个变量数组,该变量数组将存储每个关键单元格的.value或.value2属性,并将其与更改后的单元格进行比较,并以此方式触发消息框。 The problem is that code is pretty kludgey and can get bulky the more key cells you start watching. 问题在于代码很繁琐,并且您开始观看的关键单元越多,代码就会变得越笨。
A similar workaround to Roman B's suggestion, but using a global array. 与Roman B的建议类似的解决方法,但使用的是全局数组。 This stores the values in B1:B10 when you activate the sheet, then checks and updates every time a change is made in the sheet. 激活工作表时,这会将值存储在B1:B10中,然后每次在工作表中进行更改时都会检查并更新。 This may not be an ideal solution but seems like it may do the job to just check 10 cells. 这可能不是理想的解决方案,但似乎仅检查10个单元格就可以完成工作。
Option Explicit
Option Base 1
Dim globalArray(10)
Sub Worksheet_Activate()
Dim i As Long
For i = 1 To 10
globalArray(i) = Cells(i, 2)
Next i
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
For i = 1 To 10
If globalArray(i) <> Cells(i, 2) Then
' Do stuff here
'
globalArray(i) = Cells(i, 2)
End If
Next i
End Sub
It is possible to access the Formula
, Text
, Value
and Value2
properties of a cell. 可以访问单元格的Formula
, Text
, Value
和Value2
属性。 You will need to specify the Value
or Value2
of your cell, or Text
if you are after formatted text. 您需要指定Value
或Value2
你的细胞,或Text
,如果您格式化文本之后。
MSDN page has more information on these range members. MSDN页面包含有关这些范围成员的更多信息。 This page addresses difference between properties. 此页面解决了属性之间的差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.