2012-09-18 59 views
1

我試圖使用下面的宏,但它甚至沒有將其識別爲宏,因此我無法運行該宏。如果我把第一個改成只是「private/public sub test()」,它會運行,然後它說我的Target對象沒有被定義。Excel 2010宏,突出顯示有效單元格的行

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
If Target.Cells.Count > 1 Then Exit Sub 
Application.ScreenUpdating = False 
' Clear the color of all the cells 
Cells.Interior.ColorIndex = 0 
With Target 
    ' Highlight the entire row and column that contain the active cell 
    .EntireRow.Interior.ColorIndex = 8 
    .EntireColumn.Interior.ColorIndex = 8 
End With 
Application.ScreenUpdating = True 
End Sub 
+0

確保您的代碼連接到一個表,這個用來工作表一個標準的事件。 –

回答

4

你將不得不把宏的代碼片本身,而不是在一個單獨的模塊。

你在那裏做什麼是事件編程,它必須與你試圖反應的內容相匹配。

它不是你習慣的意義上的宏。事件會對發生的事情做出反應,並且無法正常運行。當您選擇另一個單元格(例如,將選擇從A1更改爲B2)時,您對所選單元格的更改作出反應的代碼。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
反應過來的時候選擇改變
If Target.Cells.Count > 1 Then Exit Sub
如果選擇了1個多細胞,然後不要運行該代碼的其餘部分的屏幕
Application.ScreenUpdating = False
關閉,所以你不能看到所有的更改,直到我們完成
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
With Target

隨着被選中的單元格,
' Highlight the entire row and column that contain the active cell
.EntireRow.Interior.ColorIndex = 8
.EntireColumn.Interior.ColorIndex = 8
End With
Application.ScreenUpdating = True

,現在所有的着色都已完成,將她切換回來,以便我們的工作結果已被看到。
End Sub

+0

欣賞回覆。我將代碼複製到「sheet1」中,並刪除了我的模塊。我按F5鍵,它仍然顯示沒有宏的宏菜單供我選擇。對不起,但我是VBA新手。 :/ – MBlackburn

+0

@ user1243498 - 這是因爲'Event'代碼只獲取事件的觸發器。在這一行放置一個斷行'如果Target.Cells。Count> 1 Then Exit Sub'然後更改頁面中的任何單元格值。你會看到你的代碼運行到斷點。然後你可以逐步完成其餘的部分。關於這個問題的rad教程存在於這裏... www.cpearson.com/excel/Events.aspx –

+0

添加代碼說明。 – SeanC

0

試試這個代碼的模塊中,把它作爲一個宏:

Public Sub Highlight() 
    Application.ScreenUpdating = False 
    ' Clear the color of all the cells 
    ActiveSheet.Cells.Interior.ColorIndex = 0 
    With ActiveCell 
     ' Highlight the entire row and column that contain the active cell 
     .EntireRow.Interior.ColorIndex = 8 
     .EntireColumn.Interior.ColorIndex = 8 
    End With 
    Application.ScreenUpdating = True 
End Sub 

你必須使用Public,所以這個子在Excel中的宏菜單變爲可用。在那裏你可以使用它'傳統' - 我明白分配一個按鈕或短鍵。

0

這個答案是基於您想要的代碼作爲事件運行的假設下,用戶改變所選單元格後

如果你想這個代碼在1個工作表中只運行然後將這個在一個工作表對象 enter image description here

Option Explicit 

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    If Target.Cells.Count > 1 Then Exit Sub 

    Application.ScreenUpdating = False 
    ' Clear the color of all the cells 
    Target.Parent.Cells.Interior.ColorIndex = 0 
    With Target 
     ' Highlight the entire row and column that contain the active cell 
     .EntireRow.Interior.ColorIndex = 8 
     .EntireColumn.Interior.ColorIndex = 8 
    End With 
    Application.ScreenUpdating = True 
End Sub 

如果你想這在所有工作表中運行,那麼把這個在ThisWorkbook對象(如果你想省略/包括你可以拍過濾紙板)

enter image description here

顯式的選項

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) 

    If Target.Cells.Count > 1 Then Exit Sub 

    Application.ScreenUpdating = False 
    ' Clear the color of all the cells 
    Sh.Cells.Interior.ColorIndex = 0 
    With Target 
     ' Highlight the entire row and column that contain the active cell 
     .EntireRow.Interior.ColorIndex = 8 
     .EntireColumn.Interior.ColorIndex = 8 
    End With 
    Application.ScreenUpdating = True 

End Sub 
相關問題