【问题标题】:Automatic Checkbox generating Excel VBA自动生成 Excel VBA 的复选框
【发布时间】:2016-06-07 21:49:07
【问题描述】:

我正在尝试自动生成复选框。如果有人在 C10 或更低的单元格中单击并写入内容,例如 C11、C12...,那么单元格的右侧应该会出现一个复选框。

它应该看起来像这样:

我应该怎么做?

更新!

    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

【问题讨论】:

  • 到目前为止你有什么尝试?任何vba代码?谢谢
  • 我唯一的问题是,如果我想删除多个单元格,复选框删除无法正常工作。

标签: vba excel checkbox


【解决方案1】:

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-29
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    相关资源
    最近更新 更多