[英]Execute excel VBA macro on cell change doesnt update
I have this code: 我有以下代码:
Sub CheckRevision()
Dim CurCell As Object
For Each CurCell In ActiveWorkbook.ActiveSheet.Range("B1:B5000")
If CurCell.Value = "Live" Then CurCell.Interior.Color = RGB(0, 204, 0)
Next
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("B1:B5000")) Is Nothing Then CheckRevision
End Sub
This works fine, however if I then have a cell which is "live" and change it back to "NOTLIVE" for example, the formatting is still a green cell. 效果很好,但是如果我有一个“活动”单元格,例如将其更改回“ NOTLIVE”,则格式仍然是绿色单元格。 How do I get it to put it back to white default?
我如何将其恢复为白色默认值?
try: (but have a look art Peh's comment) 尝试:(但请看一下Peh的评论)
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("B1:B5000")) Is Nothing Then
For Each cl In Intersect(Target, Me.Range("B1:B5000"))
If UCase(cl.Value) = "LIVE" Then
cl.Interior.Color = RGB(0, 204, 0)
Else
cl.Interior.Color = xlNone
End If
Next
End If
End Sub
Just replace line If CurCell.Value = "Live" Then CurCell.Interior.Color = RGB(0, 204, 0)
只需替换行,
If CurCell.Value = "Live" Then CurCell.Interior.Color = RGB(0, 204, 0)
with 与
If curcell.Value = "Live" Then
curcell.Interior.Color = RGB(0, 204, 0)
Else
curcell.Interior.Pattern = xlNone
End If
You don't really need the If
statement at all.. you could just use this: 您根本根本不需要
If
语句..您可以使用以下代码:
Sub CheckRevision()
Dim CurCell As Object
For Each CurCell In ActiveWorkbook.ActiveSheet.Range("B1:B5000")
CurCell.Interior.Color = xlNone - ((CurCell.Value = "Live") * (RGB(0, 204, 0) - xlNone))
Next
End Sub
So, how does this work? 那么,这是如何工作的呢? (Thanks, Pᴇʜ)
(谢谢,Pᴇʜ)
You're basically attempting to paint a cell with a colour that is either: 您基本上是在尝试用以下任一颜色绘制单元格:
xlNone or RGB(0, 204, 0)
-4142 or 52224
This is decided by the CurCell.Value = "Live"
which when used this way (cast into an integer) will return either 0
(for False
) or -1
(for True
). 这由
CurCell.Value = "Live"
决定,当以这种方式使用(转换为整数)时,它将返回0
(对于False
)或-1
(对于True
)。
Knowing that all this decision making results in a little bit of maths allows us to write an equation that causes the 0
or -1
to produce the two values: 知道所有这些决策都会导致一些数学运算,因此我们可以编写一个方程,使
0
或-1
产生两个值:
If CurCell.Value = "Live"
then the equation looks like this: 如果
CurCell.Value = "Live"
则公式如下所示:
CurCell.Interior.Color CurCell.Interior.Color
= xlNone - (-1 * (RGB(0, 204, 0) - xlNone)) = xlNone-(-1 *(RGB(0,204,0)-xlNone))
... =
xlNone - (-1 *
(RGB(0, 204, 0)
- xlNone))
... =
xlNone-(-1 *
(RGB(
0,204,0)-xlNone))
... = RGB(0, 204, 0) ... = RGB(0,204,0)
= 52224
If CurCell.Value <> "Live"
then the equation looks like this: 如果
CurCell.Value <> "Live"
则公式如下所示:
CurCell.Interior.Color = xlNone - (0 * (RGB(0, 204, 0) - xlNone)) CurCell.Interior.Color = xlNone-(0 *(RGB(0,204,0)-xlNone))
... = xlNone
- (0 * (RGB(0, 204, 0) - xlNone))
... = xlNone-
(0 *(RGB(0,204,0)-xlNone))
... = xlNone ... = xlNone
= -4142
if you aiming to change ONLY value of the cell you change the particular moment you can use the below which does not loop the whole range BUT test only the specific cell. 如果您打算仅更改单元格的值,则可以更改特定时刻,则可以使用以下内容,该方法不会循环整个范围,而仅测试特定单元格。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'Check if the cell changed included in the range B1:B5000 & if only one cell changed to avoid errors
If Not Intersect(Target, Range("B1:B5000")) Is Nothing And Target.Count = 1 Then
'Call the module to apply formatting passing 3 parameters.
Call Module1.CheckRevision(Target.Worksheet, Target.Value, Target.Address)
End If
End Sub
Sub CheckRevision(wsName As Worksheet, cellValue As String, cellAddress As String)
With wsName.Range(cellAddress)
If cellValue = "Live" Then
.Interior.Color = RGB(0, 204, 0)
Else
.Interior.Pattern = xlNone
End If
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.