如何仅突出显示最后编辑的行?
How to highlight only the last edited row?
我在 Excel 中有 table 客户数据,可以 added/edited 使用 VBA 用户表单。因此,我想突出显示刚刚 added/edited 的行。这是我第一次使用 VBA 所以我搜索并找到了来自 here:
的代码
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Value <> "" Then
Target.Interior.ColorIndex = 6
End If
End Sub
效果非常好,但之前 edits/add-ons 的亮点仍然存在。我只想突出显示最后一个。
使用变量。当您更改颜色时,将范围存储在该范围内。下次从该范围中删除颜色。
这是您正在尝试的吗?
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
这将处理评论中提到的@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
目前它将突出显示所有 sheet 中所有最后编辑的行。不确定这是否是您想要的。
- 它会在更改新范围时保持 sheet 中的彩色单元格不变。
- 它将保持其他条件格式规则不变。
- 它突出显示最后编辑的范围,即使该范围已被清除!
我在 Excel 中有 table 客户数据,可以 added/edited 使用 VBA 用户表单。因此,我想突出显示刚刚 added/edited 的行。这是我第一次使用 VBA 所以我搜索并找到了来自 here:
的代码Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Value <> "" Then
Target.Interior.ColorIndex = 6
End If
End Sub
效果非常好,但之前 edits/add-ons 的亮点仍然存在。我只想突出显示最后一个。
使用变量。当您更改颜色时,将范围存储在该范围内。下次从该范围中删除颜色。
这是您正在尝试的吗?
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
这将处理评论中提到的@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
目前它将突出显示所有 sheet 中所有最后编辑的行。不确定这是否是您想要的。
- 它会在更改新范围时保持 sheet 中的彩色单元格不变。
- 它将保持其他条件格式规则不变。
- 它突出显示最后编辑的范围,即使该范围已被清除!