2015-07-22 38 views
3

我希望用戶能夠只突出對各行突出顯示單元格中的MS Excel 2007中通過雙擊

該代碼突出了2007年的Excel單元格一個單元格,但我的問題是,我不能寫在限制用戶代碼來僅僅強調一個單元的一排,

這裏是代碼:

Private Sub Worksheet_BeforeDoubleClick(_ 


    ByVal Target As Range, Cancel As Boolean) 

' This macro is activated when you doubleclick 
' on a cell on a worksheet. 
' Purpose: color or decolor the cell when clicked on again 
' by default color number 3 is red 
     If Target.Interior.ColorIndex = 3 Then 
      ' if cell is already red, remove the color: 
      Target.Interior.ColorIndex = 2 
     Else 
      ' make the cell red: 
      Target.Interior.ColorIndex = 3 
     End If 
     ' true to cancel the 'editing' mode of a cell: 
     Cancel = True 

End Sub 

回答

2

與其將所選單元格引用存儲在單獨的或隱藏的工作表上,突出顯示的單元格引用可以存儲在內存中。他們只需在加載表格時(通過Worksheet_Activate()方法)進行初始化,否則將以類似的方式工作。

下面的代碼添加到相關表在工作簿中:

' Set of highlighted cells indexed by row number 
Dim highlightedCells As New Collection 

' Scan existing sheet for any cells coloured 'red' and initialise the 
' run-time collection of 'highlighted' cells. 
Private Sub Worksheet_Activate() 
    Dim existingHighlights As Range 
    ' Reset the collection of highlighted cells ready to rebuild it 
    Set highlightedCells = New Collection 
    ' Find the first cell that has its background coloured red 
    Application.FindFormat.Interior.ColorIndex = 3 
    Set existingHighlights = ActiveSheet.Cells.Find("", _ 
                LookIn:=xlValues, _ 
                LookAt:=xlPart, _ 
                SearchOrder:=xlByRows, _ 
                SearchDirection:=xlNext, _ 
                MatchCase:=False, _ 
                SearchFormat:=True) 
    ' Process for as long as we have more matches 
    Do While Not existingHighlights Is Nothing 
     cRow = existingHighlights.Row 
     ' Add a reference only to the first coloured cell if multiple 
     ' exist in a single row (will only occur if background manually set) 
     Err.Clear 
     On Error Resume Next 
      Call highlightedCells.Add(existingHighlights.Address, CStr(cRow)) 
     On Error GoTo 0 
     ' Search from the cell after the last match. Note an error in Excel 
     ' appears to prevent the FindNext method from finding formats correctly 
     Application.FindFormat.Interior.ColorIndex = 3 
     Set existingHighlights = ActiveSheet.Cells.Find("", _ 
                After:=existingHighlights, _ 
                LookIn:=xlValues, _ 
                LookAt:=xlPart, _ 
                SearchOrder:=xlByRows, _ 
                SearchDirection:=xlNext, _ 
                MatchCase:=False, _ 
                SearchFormat:=True) 
     ' Abort the search if we've looped back to the top of the sheet 
     If (existingHighlights.Row < cRow) Then 
      Exit Do 
     End If 
    Loop 

End Sub 


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 
    Dim hCell As String 
    Dim cellAlreadyHighlighted As Boolean 
    hCell = "" 

    Err.Clear 
    On Error Resume Next 
     hCell = highlightedCells.Item(CStr(Target.Row)) 
    On Error GoTo 0 

    If (hCell <> "") Then 
     ActiveSheet.Range(hCell).Interior.ColorIndex = 0 
     If (hCell = Target.Address) Then 
      Call highlightedCells.Remove(CStr(Target.Row)) 
      Target.Interior.ColorIndex = 0 
     Else 
      Call highlightedCells.Remove(CStr(Target.Row)) 
      Call highlightedCells.Add(Target.Address, CStr(Target.Row)) 
      Target.Interior.ColorIndex = 3 
     End If 
    Else 
     Err.Clear 
     On Error Resume Next 
      highlightedCells.Remove (CStr(Target.Row)) 
     On Error GoTo 0 
     Call highlightedCells.Add(Target.Address, CStr(Target.Row)) 
     Target.Interior.ColorIndex = 3 
    End If 
    Cancel = True 
End Sub 
+0

非常感謝,現在的工作:) –

+0

很高興聽到@Mohammad Mbydeen。也許你可以對它表示感謝:) – VirtualMichael

0

建議你使用Worksheet_BeforeDoubleClick方法通過將雙來跟蹤「突出」細胞然後單擊隱藏表單上的單元格引用在事件處理程序中使用條件格式或顯式檢查來根據隱藏工作表上的值突出顯示相關單元格(或「單元格」,如果您允許選擇多行上的單個單元格)。如果您選擇使用條件格式設置,則無論何時「雙擊」新單元格,引用都將在隱藏表格上更新,條件格式將自動重新計算。給定行上只有一個單元格將永遠保持「突出顯示」狀態。

或者,您也可以通過沿線的調整您雙擊事件處理代碼做明確如下:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 
    If (Not (IsEmpty(Worksheets("Sheet2").Cells(1, 1).Value))) Then 
     ActiveSheet.Range(Worksheets("Sheet2").Cells(1, 1).Value).Interior.ColorIndex = 0 
    End If 
    Worksheets("Sheet2").Cells(1, 1).Value = Target.Address 
    ActiveSheet.Range(Worksheets("Sheet2").Cells(1, 1).Value).Interior.ColorIndex = 3 
End Sub 

這種方式,你也可以檢查在加載紙張任何突出顯示單元格並在適當的情況下重置它們(假設允許用戶保存更改)。

爲了突出在任何給定的行只有一個單元格(但允許多行有一個單一的高亮單元格),你可以使用下面的(這也將切換亮點已經突出顯示單元):

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 
    If (Not (IsEmpty(Worksheets("Sheet2").Cells(Target.Row, 1).Value))) Then 
     ActiveSheet.Range(Worksheets("Sheet2").Cells(Target.Row, 1).Value).Interior.ColorIndex = 0 
     If (Worksheets("Sheet2").Cells(Target.Row, 1).Value = Target.Address) Then 
      Worksheets("Sheet2").Cells(Target.Row, 1).Value = "" 
      Target.Interior.ColorIndex = 0 
     Else 
      Worksheets("Sheet2").Cells(Target.Row, 1).Value = Target.Address 
      Target.Interior.ColorIndex = 3 
     End If 
    Else 
     Worksheets("Sheet2").Cells(Target.Row, 1).Value = Target.Address 
     Target.Interior.ColorIndex = 3 
    End If 
    Cancel = True 
End Sub 
+0

它的工作,但其放置在該行的第一個單元格突出顯示單元格的引用,我不希望出現這種情況,請問你有另一種解決方案嗎? –

+0

@Mohammad Mbydeen - 我發佈了一個替代方案,將突出顯示的單元格存儲在內存中作爲單獨的答案。 – VirtualMichael

0

試試這個:

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

    'Target must be between column "A" which is 1 & "G" which is 7 and also between row 1 and 10. 
    'I also add checking for row. If you don't need, remove it. 
    If Target.Column >= 1 And Target.Column <= 7 And Target.row >= 1 And Target.row <= 10 Then 

     If Target.Interior.ColorIndex = 3 Then 
      ' if cell is already red, remove the color: 
      Target.Interior.ColorIndex = 2 
     Else 
      ' make the cell red: 
      Target.Interior.ColorIndex = 3 
     End If 

     ' true to cancel the 'editing' mode of a cell: 
     Cancel = True 

    End If 

End Sub 
+0

謝謝!但是這段代碼只用於一列,我想要一系列行,請幫忙? –

+0

不清楚你的要求。用一些例子來說更多。我會嘗試。 –

+0

這裏是問題,我有一個白色背景的表,用戶可以通過單擊分配給宏的按鈕向表中添加行和列,我希望用戶能夠突出顯示錶格中的單元格,因爲桌子的背景是白色的,整張桌子的背景都是灰色的,用你的代碼改變整行的背景。桌子是從A:G開始的,可以延伸 –

1

我相信你想要的單元格顏色恢復到正常細胞,用白色背景沒有特別填充它。

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

    Cancel = True 
    Dim iCOLOR As Long 
    If Target.Interior.ColorIndex <> 3 Then _ 
     iCOLOR = 3 
    Rows(Target.Row).Interior.Pattern = xlNone 
    If iCOLOR = 3 Then _ 
     Target.Interior.ColorIndex = iCOLOR 

End Sub 

刪除填充的方法是設置.Interior.Pattern = xlNone

如果在不是紅色的情況下需要填充純白色的細胞填充物,則可以使用此功能打開和關閉。

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

    Cancel = True 
    Dim iCOLOR As Long 
    iCOLOR = 3 + CBool(Target.Interior.ColorIndex = 3) 
    Rows(Target.Row).Cells.Interior.ColorIndex = 2 
    Target.Interior.ColorIndex = iCOLOR 

End Sub 

當然,ListObject提出了一組不同的問題。

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

    If Not Intersect(Target, ListObjects("Table1").DataBodyRange) Is Nothing Then 
     Cancel = True 
     Dim iCOLOR As Long 
     iCOLOR = 3 + CBool(Target.Interior.ColorIndex = 3) 
     Intersect(Rows(Target.Row), ListObjects("Table1").DataBodyRange).Interior.ColorIndex = 2 
     Target.Interior.ColorIndex = iCOLOR 
    End If 

End Sub 
+0

謝謝,但您的代碼更改了表格背景和工作表中整個行的背景,我不想更改整行的背景。當用戶點擊以突出顯示單元格時,它會突出顯示爲紅色,並且會降低顯示爲白色。請你能幫忙嗎? –

+0

目前還不清楚你想將單元格填充設置爲多寬,但如果這是首選,那麼我已經調整了上面的代碼以將整行填充爲白色,以便兩個單元格不能同時在相同的時間處於紅色行。 – Jeeped

+0

這裏是問題所在,我有一個帶有白色背景的表格,用戶可以通過單擊分配給宏的按鈕向表中添加行和列,我希望用戶能夠突出顯示錶格中的單元格,因爲桌子的背景是白色的,整個桌子的背景是灰色的,用你的代碼改變整行的背景。桌子是從A:G開始的,可以擴展 –

相關問題