2016-06-07 71 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