简体   繁体   English

查看单元格输出而不是公式的宏

[英]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. 可以访问单元格的FormulaTextValueValue2属性。 You will need to specify the Value or Value2 of your cell, or Text if you are after formatted text. 您需要指定ValueValue2你的细胞,或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.

相关问题 有没有办法使用单元格中的格式化 output 而不是未格式化的值? - Is there a way to use the formatted output from a cell, rather than the unformatted value? 如何使用vlookup搜索单元格值而不是其单元格引用公式(Excel) - How to use vlookup to search for a cell value rather than its cell reference formula(Excel) 使用宏将公式添加到单元格 - Adding Formula to a cell using macro Excel宏:设置单元格的公式 - Excel Macro: Set the formula of a cell 复制一个工作表中的单元格范围,并将其作为值而不是公式粘贴到另一个工作表中 - Copy cell range from one worksheet and paste in a different worksheet as a value rather than formula Excel VBA 模块在整列中插入公式,而不是在行中插入单个单元格 - Excel VBA Module is inserting formula in entire column rather than single cell in row Excel VBA - 如何引用单元格的可见内容而不是其公式? - Excel VBA - How do I refer to a cell's visible content rather than its formula? 在Excel和VBA中,如何将单元格的值设置为公式结果而不是公式 - In Excel and VBA, how to set value of cell to the formulas result rather than the formula 当处理具有单元格引用的条件公式而不是固定数字时,“&”的含义是什么 - What is the meaning of “&” when dealing with condition formula with cell reference rather than fixed numbers 根据另一个单元格中的值设置公式要查看的单元格? - set the cell a formula looks at, based on a value in another cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM