2017-04-06 78 views
1

我有一個條件格式的工作簿:條件格式不斷刪除?

enter image description here

條件格式要突出我的範圍內的:P爲取決於N列的狀態

我已經設置了範圍的每一行:

enter image description here

條件格式正常工作的大部分時間。

但是,如果用戶複製並粘貼到單元格中,條件格式將丟失。

我也有這個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 

請有人能告訴我我做錯了什麼?

+0

複製並粘貼也會複製源單元格中的格式。您需要複製並「粘貼特殊 - >值」以防止條件格式化被清除。發生這種情況非常令人討厭。你的代碼似乎不是問題。 – Enigmativity

+0

@Enigmativity這是一個共享工作簿,我無法控制用戶在複製和粘貼時的操作。有沒有一種方法可以放置一個只允許粘貼值的rule/vba代碼? – user7415328

回答

0

如果我理解正確的話,你就與條件格式問題,如果從一個小區的用戶粘貼被刪除不相同的條件格式規則。這會將有條件格式化的單元格重新設置爲與插入之前具有相同格式的範圍內的所有單元格。

下面只被在一個小範圍內測試= A2:P4因此它可能不會很好地擴展到片材的每一行。附加說明和免責聲明的代碼之後跟隨:

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 

我已經明確地定義首先/最後一個行/列,因爲CondFormat.AppliesTo返回的第一個地址可能不包含在左上角和右下角的細胞。我希望新的CondFormat.AppliesTo成爲一個單一的區域。

要獲得LASTROW /列我通過引起粘貼單元格的塊狀地區不得不循環。

Target.EntireColumn.FormatConditions.Count將返回應用於列中任何單元格的最大數量。請注意,如果在更改的列中有兩組不同的範圍應用了兩組不同的條件格式 - 它們將被複制到兩個範圍內的所有單元格。結果可能無法預測!

0

嘿一般來說,這不會發生,只要你複製粘貼&永遠的Excel拷貝格式也。您最好使用選擇性粘貼而不是粘貼。 你也試試這個代碼,

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 
1

你需要的是保護表格式,而不保護細胞,使用戶可以粘貼但不影響數據。以下是如何做到這一點:

選擇所有你想要編輯的細胞(比如,幾列),並點擊右鍵 - >設置單元格格式 - >保護,並取消選中「鎖定」。

enter image description here

然後去審查選項卡,選擇保護工作表。確保格式單元格未被選中。

enter image description here

現在用戶可以輸入數據,但格式已被鎖定。更多here