2014-03-03 79 views
0

我注意到規則說明不要求澄清別人的問題,所以希望這是做到這一點的正確方法。我最初找到足夠的答案讓我在Change color of cell with mouse click in Excel上。謝謝user3159079和tigeravatar。通過doubleclick着色單元格

我有這樣的:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)  
    Cancel = True 
    Worksheet_SelectionChange Target 
End Sub 

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 change the background to the specified color 
     Target.Interior.ColorIndex = 6 

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

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

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

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

    End If 
End Sub 

,但我有3個問題。

1)我想指定幾個範圍,這個工作不會影響其他細胞(即我想它去努力......

$F$4:$F$6 
$D$10:$I$12 
$F$17:$I$34 
$N$5:$O$6 
$N$10:$O$11 
$O$15:$P$18 
$O$24:$P$24 
$O$29:$P$29 
$O$34:$P$34 
$U$6:$X$7 
$U$10:$X$14 
$AA$6:$AG$8 
$F$38:$F$43 
$N$38:$N$44 
$E$48:$E$51 
$Q$48:$R$51 
$X$23:$AG$35 

...和其他地方。

2)我希望這隻能在雙擊時工作,而不是在第一次單擊時改變單元格

3)這個工作,直到我保存,關閉並重新打開電子表格。重新打開工作表後,點擊功能上的顏色消失。

我對這些都不是很瞭解,但是我能夠很好地搜索,這是我得到這個目的的方式,但是我無法想象它會如何幫助,我們將不勝感激。

回答

2

我在下面修改了您的代碼以滿足1)和2)的要求。

對於要求3):將電子表格保存爲.xlsm格式,並且一旦再次打開它,允許運行宏。

讓我知道如何去:

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

    '(1) Desired Range where it works: 
    Set MyRange = Range("$F$4:$F$6,$D$10:$I$12,$F$17:$I$34,$N$5:$O$6," & _ 
         "$N$10:$O$11,$O$15:$P$18,$O$24:$P$24,$O$29:$P$29," & _ 
         "$O$34:$P$34,$U$6:$X$7,$U$10:$X$14,$AA$6:$AG$8," & _ 
         "$F$38:$F$43,$N$38:$N$44,$E$48:$E$51,$Q$48:$R$51," & _ 
         "$X$23:$AG$35") 
    Cancel = True 

    '(1) Check if double clicked cell is one where the code should work: 
    If Not Intersect(Target, MyRange) Is Nothing Then 
     Custom_ColourChange Target 
    End If 
End Sub 

'(2) Changed from default Worksheet_Selection event to Custom Sub: 
Private Sub Custom_ColourChange(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 change the background to the specified color 
     Target.Interior.ColorIndex = 6 

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

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

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

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

    End If 
End Sub 

編輯:

編輯以下@ BK201和@simoco評論

+1

而不是遍歷'MyRange',將不檢查'如果不相交(...)沒有什麼證明更好? – Manhattan

+0

當然!我只是記不起正確的方式來做到這一點。謝謝。 – hstay

+0

+1。簡短而甜美。 :) – Manhattan