2017-06-14 80 views
0

當雙擊單元格時,我使用下面的VBA代碼顯示覆選標記/打勾。有時候,勾選/勾號需要刪除。要刪除勾號/勾號,單元格需要再次雙擊。一旦雙擊刪除複選標記/勾號,單元格將返回爲空白。我的問題是;是否可以顯示單詞「是」而不是讓單元格變爲空白?VBA選中標記 - 如果雙擊並刪除,則返回「是」

或者作爲更好的選擇,是否可以在單元格中雙擊(刪除複選標記/複選框)而不是返回空白時返回原始文本(在單元格被雙擊之前)?

我希望這是有道理的!先謝謝你!

下面是我已經用於複選標記VBA代碼/蜱:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 
    On Error GoTo 1 
    If Not Intersect(Target, Range("A2:Z200")) Is Nothing Then 
     Application.EnableEvents = False 
     If Target.Value = ChrW(&H2713) Then 
      Target.ClearContents 
      Cancel = True 
     Else 
      Target.Value = ChrW(&H2713) 
      Cancel = True 
     End If 
    End If 

    On Error GoTo 0 
1 Application.EnableEvents = True 

End Sub 

回答

0

要顯示單詞 「YES」 改變線:

Target.ClearContents 

隨着:

Target.Value = "YES" 

Full Code

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

    On Error GoTo 1 
    If Not Intersect(Target, Range("A2:Z200")) Is Nothing Then 
     Application.EnableEvents = False 
     If Target.Value = ChrW(&H2713) Then 
      Target.Value = "YES" 
     Else 
      Target.Value = ChrW(&H2713) 
     End If 
     Cancel = True 
    End If 

    On Error GoTo 0 
1 Application.EnableEvents = True 

End Sub 
0

這將有標記替換原來的值,然後恢復上再點擊原來的值,但它僅適用於文本值,只有改變細胞的外觀 ...

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

    If Not Intersect(Target, Range("A2:Z200")) Is Nothing Then 
     On Error GoTo haveError 
     Application.EnableEvents = False 
     If Target.NumberFormat Like ";;;*" Then 
      Target.NumberFormat = "General" 
     Else 
      Target.NumberFormat = ";;;" & ChrW(&H2713) 
     End If 
     Cancel = True 
    End If 

haveError: 
    Application.EnableEvents = True 

End Sub