条件格式不断删除?

我有一个带有条件格式的工作簿:

在这里输入图像说明

条件格式应根据列N中的状态突出显示范围A:P。

我已经设定了范围:

在这里输入图像说明

条件格式大多数情况下工作正常。

但是,如果用户复制并粘贴到单元格中,条件格式将丢失。

我也有这个vba代码查找复制到单元格的值,并从另一个工作簿中查找相应的值。 不知道这是否与删除的条件格式有关。

'Insert Depot Memo Data for user Dim oCell As Range, targetCell As Range Dim ws2 As Worksheet On Error GoTo Message If Not Intersect(Target, Range("B:B")) Is Nothing Then ' <-- run this code only if a value in column B has changed If Not GetWb("Depot Memo", ws2) Then Exit Sub With ws2 For Each targetCell In Target Set oCell = .Range("J1", .Cells(.Rows.Count, "J").End(xlUp)).Find(What:=targetCell.Value, LookIn:=xlValues, LookAt:=xlWhole) If Not oCell Is Nothing Then Application.EnableEvents = False 'Set Format of cell targetCell.ClearFormats targetCell.Font.Name = "Arial" targetCell.Font.Size = "10" targetCell.Font.Color = RGB(128, 128, 128) targetCell.HorizontalAlignment = xlCenter targetCell.VerticalAlignment = xlCenter targetCell.Borders(xlEdgeBottom).LineStyle = xlContinuous targetCell.Borders(xlEdgeTop).LineStyle = xlContinuous targetCell.Borders.Color = RGB(166, 166, 166) targetCell.Borders.Weight = xlThin targetCell.Offset(0, -1).Value = Now() targetCell.Offset(0, 1).Value = oCell.Offset(0, 1) targetCell.Offset(0, 2).Value = oCell.Offset(0, -2) targetCell.Offset(0, 3).Value = oCell.Offset(0, -7) Application.EnableEvents = True End If Next End With End If 

请有人能告诉我我做错了什么?

如果我理解正确,如果用户从单元格粘贴而没有相同的条件格式规则,则会出现条件格式被删除的问题。 这将重新设置有条件格式化的单元格到与插入之前具有相同格式的范围内的所有单元格。

以下仅在小范围= A2:P4进行了testing,因此可能无法很好地扩展到纸张的每一行。 代码后面附加了解释和免责声明:

 Private Sub Worksheet_Change(ByVal Target As Range) Dim FirstRow As Long Dim FirstColumn As Long Dim LastRow As Long Dim LastColumn As Long Dim c As Range Dim LastCondFormatCell As Range Dim CondFormat As FormatCondition Dim i As Range Application.EnableEvents = False Application.ScreenUpdating = False If Target.EntireColumn.FormatConditions.Count > 0 Then For Each CondFormat In Target.EntireColumn.FormatConditions Set c = CondFormat.AppliesTo If Not c Is Nothing Then FirstRow = c.SpecialCells(xlCellTypeAllFormatConditions).Row FirstColumn = c.SpecialCells(xlCellTypeAllFormatConditions).Column For Each i In c.Areas LastRow = WorksheetFunction.Max(i.Row + i.Rows.Count - 1, LastRow) LastColumn = WorksheetFunction.Max(i.Column + i.Columns.Count - 1, LastColumn) Next Set LastCondFormatCell = Cells(LastRow, LastColumn) CondFormat.ModifyAppliesToRange Range(Cells(FirstRow, FirstColumn).Address, _ LastCondFormatCell.Address) End If Set c = Nothing Set i = Nothing Next End If Application.ScreenUpdating = True Application.EnableEvents = True End Sub 

我不得不显式定义First / Last Row / Column,因为CondFormat.AppliesTo返回的第一个地址可能不包含左上angular和右下angular的单元格。 我希望新的CondFormat.AppliesTo成为一个单独的区域。

为了得到LastRow / Column,我必须遍历由粘贴的单元格造成的碎片区域。

Target.EntireColumn.FormatConditions.Count将返回应用于列中任何单元格的最大数量。 请注意,如果在更改的列中应用了不同范围的两组不同的条件格式,它们将被复制到两个范围中的所有单元格。 结果可能是不可预知的!

你想要的是保护表格格式,而不保护单元格,所以用户可以粘贴但不影响数据。 以下是如何做到这一点:

select你想要编辑的所有单元格(比如几列),然后右键单击 – >格式化单元格 – >保护,并取消选中“locking”。

在这里输入图像说明

然后转到审查选项卡,select保护工作表。 确保格式单元格未被选中。

在这里输入图像说明

现在用户可以input数据,但格式化被locking。 更多在这里 。

嘿通常这从来不会发生,每当你复制和粘贴Excel总是复制格式也。 您最好使用select性粘贴,而不是粘贴。 你也试试这个代码,

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) With Sheets("Sheet3") .Visible = xlVeryHidden .Range("B2:C2").Copy End With Me.Range("B2").PasteSpecial xlPasteFormats Application.CutCopyMode = False End Sub