2016-06-07 108 views
0

我試圖自動化複選框生成。如果有人在C10單元格中點擊或寫入某些內容,或者像C11,C12 ...那樣,則單元格右側應顯示一個複選框。自動複選框生成Excel VBA

它應該是這個樣子:

Checkbox

我應該怎麼辦呢?

UPDATE!:

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim chkbox As CheckBox 

    If Not (Intersect(Target, Range("C10:C1000")) Is Nothing) Then 
     If Not (IsEmpty(Target.Cells.Value)) Then 
      'If the cell is NOT empy, I should add a checkbox, to the right of the cell without text 
      Set chkbox = ActiveSheet.CheckBoxes.Add(Target.Left, Target.Top, Target.Width, Target.Height) 
      With chkbox 
       .Text = "" 
      End With 
     Else 
      For Each chkbox In ActiveSheet.CheckBoxes 
       If Not Intersect(Target, chkbox.TopLeftCell) Is Nothing Then 
        chkbox.Delete 
       End If 
      Next chkbox 
     End If 
    End If 
End Sub 
+0

你有什麼嘗試這麼遠嗎?任何vba代碼?謝謝 –

+0

我加了代碼:) – Twi

+0

我唯一的問題是,如果我想刪除多個單元格,checbox刪除工作不正常。 – Twi

回答

0

試試這個

Option Explicit 

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim chkbox As CheckBox 
    Dim cell As Range 

    If Not Intersect(Target, Range("C10:C1000")) Is Nothing Then 
     For Each cell In Intersect(Target, Range("C10:C1000")) 
      If Not IsEmpty(cell.Value) Then 
       'If the cell is NOT empy, I should add a checkbox, to the right of the cell without text 
       Set chkbox = ActiveSheet.CheckBoxes.Add(cell.Left, cell.Top, cell.Width, cell.Height) 
       With chkbox 
        .Text = "" 
       End With 
      Else 
       For Each chkbox In ActiveSheet.CheckBoxes 
        If Not Intersect(cell, chkbox.TopLeftCell) Is Nothing Then 
         chkbox.Delete 
        End If 
       Next chkbox 
      End If 
     Next cell 
    End If 
End Sub 
+0

它完美無缺地工作! – Twi

+0

好!很高興它的工作 – user3598756