Excel VBA - 如果单元格包含复选框,则为Ckeck

时间:2016-03-10 16:15:23

标签: excel vba

我使用以下脚本将复选框添加到一系列单元格中。我想通过检查单元格是否已经包含一个复选框来增强此功能,如果它确实没有在单元格中添加新的复选框,即只有复选框添加到该范围内的单元格(如果它尚未包含复选框)

    LRow = ActiveSheet.Range("D" & Rows.Count).End(xlUp).Row

For cell = 10 To LRow
        CLeft = Cells(cell, "R").Left
        CTop = Cells(cell, "R").Top
        CHeight = Cells(cell, "R").Height
        CWidth = Cells(cell, "R").Width
        ActiveSheet.CheckBoxes.Add(CLeft, CTop, CWidth, CHeight).Select
        With Selection
            .Caption = ""
            .Value = xlOff
            .Display3DShading = False
        End With
Next cell

1 个答案:

答案 0 :(得分:1)

使用此功能检查范围是否包含复选框:

Public Function HasCheckbox(rng As Range) As Boolean
    For Each CB In ActiveSheet.CheckBoxes
        If Not Application.Intersect(rng, CB.TopLeftCell) Is Nothing Then
            HasCheckbox = True
            Exit Function
        End If
    Next CB
    HasCheckbox = False
End Function