Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何仅高亮显示最后编辑的行?_Excel_Vba_Highlight - Fatal编程技术网

Excel 如何仅高亮显示最后编辑的行?

Excel 如何仅高亮显示最后编辑的行?,excel,vba,highlight,Excel,Vba,Highlight,我有一个Excel中的客户数据表,可以使用VBA用户表单添加/编辑。因此,我想突出显示刚刚添加/编辑的行。这是我第一次使用VBA,因此我搜索并找到了以下代码: Private子工作簿\u SheetChange(ByVal Sh作为对象,ByVal Target作为范围) 如果Target.Value为“”,则 Target.Interior.ColorIndex=6 如果结束 端接头 虽然效果非常好,但之前编辑/附加组件的高光仍然存在。我只想突出显示最后一个。使用变量。更改颜色时,将范围存储

我有一个Excel中的客户数据表,可以使用VBA用户表单添加/编辑。因此,我想突出显示刚刚添加/编辑的行。这是我第一次使用VBA,因此我搜索并找到了以下代码:

Private子工作簿\u SheetChange(ByVal Sh作为对象,ByVal Target作为范围)
如果Target.Value为“”,则
Target.Interior.ColorIndex=6
如果结束
端接头

虽然效果非常好,但之前编辑/附加组件的高光仍然存在。我只想突出显示最后一个。

使用变量。更改颜色时,将范围存储在该区域中。下次删除该范围内的颜色

这就是你想要的吗

Dim prevRng As Range

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim aCell As Range

    If Not prevRng Is Nothing Then prevRng.Interior.ColorIndex = xlNone
    Set prevRng = Target

    For Each aCell In Target
        If aCell.Value <> "" Then aCell.Interior.ColorIndex = 6
    Next aCell
End Sub
Dim preverng As范围
私有子工作簿(ByVal Sh作为对象,ByVal目标作为范围)
Dim aCell As系列
如果Not prevRng为Nothing,则prevRng.Interior.ColorIndex=xlNone
设置prevRng=Target
对于目标中的每个aCell
如果aCell.Value为“”,则aCell.Interior.ColorIndex=6
下一个亚塞尔
端接头

这将作为@p处理多个单元格ᴇʜ在评论中提到。

以下是一些适用于您的代码:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

'Removing old conditional formatting if it exists already
Conditions = ActiveSheet.Cells.FormatConditions.Count
For i = 1 to Conditions
    If ActiveSheet.Cells.FormatConditions(i).Type = 2 Then
        If ActiveSheet.Cells.FormatConditions(i).Formula1 = "=1" Then ActiveSheet.Cells.FormatConditions(i).Delete
    End If
Next i

'Adding new conditional formatting rule to the edited range
Target.EntireRow.FormatConditions.Add Type:=xlExpression, Formula1:="=1"
Target.EntireRow.FormatConditions(1).Interior.ColorIndex = 6

End Sub
当前,它将高亮显示所有图纸中最后编辑的所有行。不确定这是否是你想要的

  • 当一个新的范围被改变时,它会保持你工作表中的彩色单元格完好无损
  • 它将保持其他条件格式规则不变
  • 它高亮显示上次编辑的范围,即使该范围已被清除
您需要一个循环,以便
Target
在其单元格中循环。它可以是一个范围而不是单个单元格,然后
Target.Value”“
失败。如果。。。将合并。。。请稍等,我想问一下,为什么要添加
If
语句?我的意思是,即使在一个单元格上,循环也会在
Target
上运行。这就是性能上的差异吗?还是我错过了什么?我现在在办公室,所以现在不能抽出时间来做这件事。稍后将重新阅读此帖子tonight@SiddharthRout如果只需要处理一个特定的表/工作表,那么您可以将
工作簿\u SheetChange
切换到特定的
工作表\u Change
事件,这样您的解决方案就会合适(除了我描述的保存效果)。这将把问题简化为一个更简单的问题。要么向后运行循环(
For i=Conditions to 1 Step-1
),要么在
If
语句中包含
For
的退出,否则,当
“=1”
条件不是最后一个时,您可能会得到一个错误。@Chronocidal,谢谢您的帮助。我已经用另一个if语句更新了代码,因为反向迭代和退出策略都起作用并产生了另一个错误。有没有理论解释为什么
语句也不起作用?
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

'Removing old conditional formatting if it exists already
Conditions = ActiveSheet.Cells.FormatConditions.Count
For i = 1 to Conditions
    If ActiveSheet.Cells.FormatConditions(i).Type = 2 Then
        If ActiveSheet.Cells.FormatConditions(i).Formula1 = "=1" Then ActiveSheet.Cells.FormatConditions(i).Delete
    End If
Next i

'Adding new conditional formatting rule to the edited range
Target.EntireRow.FormatConditions.Add Type:=xlExpression, Formula1:="=1"
Target.EntireRow.FormatConditions(1).Interior.ColorIndex = 6

End Sub