2017-04-06 7 views
1

私は条件付き書式設定ブック有する:条件付き書式は引き続き削除されますか?

enter image description here

条件付き書式は、私の範囲A強調すべきである:カラムN.で状況に応じて行ごとにP

を私は範囲を設定しています。

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これは共有ブックであり、コピーして貼り付けるときのユーザーの操作を制御できません。値の貼り付けだけを許可するルール/ vbaコードを配置する方法はありますか? – user7415328

答えて

0

私が正しく理解していれば、ユーザーが同じ条件付き書式のルールなしでセルからペーストすると、条件付き書式が削除されるという問題があります。これにより、条件付きでフォーマットされたセルが挿入前と同じフォーマットを持つ範囲内のすべてのセルに再配置されます。

以下は小範囲= A2:P4でのみテストされているため、シートの各行にうまく調整できない可能性があります。追加の説明や免責事項は、コードの後に​​続く:私はCondFormat.AppliesToによって返された最初のアドレスは、左上と右下のセルが含まれていない可能性があるため、明示的に姓/行/列を定義するために持っていた

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を1つのエリアにしたいと思っていました。

LastRow/Columnを取得するには、貼り付けられたセルによって引き起こされた断片化された領域をループする必要がありました。

Target.EntireColumn.FormatConditions.Countは、列の任意のセルに適用される最大数を返します。変更された列の異なる範囲に適用される2つの異なる条件付きフォーマットがある場合は、両方の範囲のすべてのセルにコピーされます。結果は予測できないでしょう!

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

今ユーザーがデータを入力することができますが、書式設定はロックされています。 More here