2013-01-08 58 views
1

我有這個用於在用戶插入C或P時在Excel工作表上輸入評論,並且我需要在編輯後隱藏評論。Excel宏 - 編輯後隱藏評論

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim KeyCells As Range 


    Set KeyCells = Range("A1:S1000") 

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _ 
      Is Nothing Then 

      Select Case Range(Target.Address) 
       Case "C": 
        minhaCelula = Target.Address 
        Range(minhaCelula).AddComment ("") 
        Range(minhaCelula).Comment.Visible = True 
       Case "P": 
        minhaCelula = Target.Address 
        Range(minhaCelula).AddComment ("") 
        Range(minhaCelula).Comment.Visible = True 

      End Select 


    End If 
End Sub 

回答

2

一些問題與代碼:

  • Select Case Range(Target.Address)沒有意義 - 它採用Target範圍,需要其地址並建立來自該地址的範圍,它指向原來Target範圍,最後VB取得該範圍的默認屬性,因爲它沒有在對象引用上下文中使用。所以應該用Target.Value代替整個事情。
  • 後來同樣的事情發生在minhaCelula
  • "C""P"下的代碼是一樣的,應該放在同一個Case分支下。
  • 冒號未在VB的Select Case中使用。
  • AddComment應該叫做without parentheses。更好的是,您應該從AddComment返回對添加的註釋的引用中獲益,因此您可以直接使用它(在這種情況下,您必須保留括號)。

    Private Sub Worksheet_Change(ByVal Target As Range) 
        If Not Application.Intersect(Me.Range("A1:S1000"), Target) Is Nothing Then 
         Select Case Target.Value 
          Case "C", "P" 
           Target.AddComment("").Visible = True 
         End Select 
        End If 
    End Sub 
    

    至於這個問題,當您使用Comment.Visible,Excel停止管理評論的可見性:爲

所以應該rewriteen。要將管理員留在Excel側,請改變評論的形狀:

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Not Application.Intersect(Me.Range("A1:S1000"), Target) Is Nothing Then 
     Select Case Target.Value 
      Case "C", "P" 
       With Target.AddComment("").Shape 
        .Visible = msoTrue 
        .Select 
       End With 
     End Select 
    End If 
End Sub