检查单元格是否为空 Excel VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18766590/
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
Check if cell was empty Excel VBA
提问by MZimmerman6
I am writing an Excel Macro to display a warning if the user edits a cell that was previously empty. Basically if a cell is edited, is in column 1, and already contains text I want to display a warning But if it does not have text already, I do not want to.
如果用户编辑以前为空的单元格,我正在编写一个 Excel 宏以显示警告。基本上,如果一个单元格被编辑,在第 1 列中,并且已经包含文本我想显示警告但如果它没有文本,我不想。
So what I have tried is the following
所以我尝试过的是以下内容
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Not IsEmpty(Cells(Target.Row,Target.Column)) Then
Application.EnableEvents = False
MsgBox "Some Message"
Application.EnableEvents = True
End If
End Sub
The issue I am having is when I am getting the cell to see if it was empty or not, it will never return that it was because, well the user just added stuff to it.
我遇到的问题是,当我让单元格查看它是否为空时,它永远不会返回它,因为用户只是向其中添加了内容。
So I want to know if there is a simple way to check the previous state of that cell. I want to find out if the cell WAS empty. Is this possible?
所以我想知道是否有一种简单的方法可以检查该单元格的先前状态。我想知道单元格是否为空。这可能吗?
回答by Declan_K
You could use the Worksheet_SelectionChange
event to capture the value of the cell that the user has selected in a variable, which happens before they make any changes.
您可以使用该Worksheet_SelectionChange
事件来捕获用户在变量中选择的单元格的值,这在他们进行任何更改之前发生。
Then when Worksheet_Change
is fired, you can refer to that variable.
然后当Worksheet_Change
被触发时,您可以引用该变量。