2012-01-09 18 views
0

Excel 2003では、マウスのクリックだけでセルの色を変更したいと思います。これは私がやったことですが、私の質問は、セルを2回目のマウスクリックで元の色に戻す方法です。私は、セルの色を白から緑に変更し、各マウスクリックで再び元に戻したいと考えています。マウスのクリックでセルの色を変更する

+1

Plsはご返信用 – brettdj

答えて

3

これは私が自分のシートの1つに使用しているものです。基本的に、ユーザーはセルをダブルクリックしてユーザーフォームを表示します。彼らは補正を提出するときに黄色のセルを強調表示します。間違えた場合は、ダブルクリックしてハイライトを削除します。必要なものについては、以下のコードの中程を抜け出すことができます。以下のコメントあたり

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 

Application.EnableEvents = False 

    Dim TargRow As Variant 
    Dim TargCol As Variant 

    TargRow = Target.Row 
    TargCol = Target.Column 

    Header = 8 
    FirstCol = 0 
    LastCol = 13 
    CommentCol = 13 

    If TargRow > Header And TargCol > FirstCol And TargCol < LastCol Then 
     'If the cell is clear 
     If Target.Interior.ColorIndex = xlNone Then 
      Cancel = True 

      'Then change the background to yellow 
      Target.Interior.ColorIndex = 6 
      Corrections.Show 

      'Else if the cell background color is already yellow 
      ElseIf Target.Interior.ColorIndex = 6 Then 

      'Then clear the background 
      Target.Interior.ColorIndex = xlNone 
     End If 
    End If 

    'This is to prevent the cell from being edited when double-clicked 
    Cancel = True 

    Application.EnableEvents = True 
End Sub 

EDITED CODE:

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

    'If the target cell is clear 
    If Target.Interior.ColorIndex = xlNone Then 

     'Then change the background to the specified color 
     Target.Interior.ColorIndex = 4 

     'But if the target cell is already the specified color 
     ElseIf Target.Interior.ColorIndex = 4 Then 

     'Then clear the background color 
     Target.Interior.ColorIndex = xlNone 

    End If 

End Sub 
+0

おかげで、私はまだ問題を抱えている( 'これ私が行っている' で暗示)あなたの現在のコードを投稿してください。私はこれを特定の細胞に適用しようとしています。私の今の所は次の通りです: –

+0

私は上記のものを切り刻んで、どこに置くかを変えました。コードは、ワークシート上のSelectionChangeイベントとして配置されます。セルをクリックするだけです(上記のようにダブルクリックしないでください)。セルをクリックするとセルが緑色に変わり、セルをシングルクリックすると再びカラーがクリアされます。また、上記の私の元の答えとは異なり、細胞を編集することができます。コードについては上記の編集済みの回答を参照してください。 – Jon

関連する問題