Excel VBA – Cckck如果单元格包含checkbox

我正在使用以下脚本将checkbox添加到单元格的范围。 我想通过检查单元格是否已经包含一个checkbox来增强它,如果它没有,那么不要在单元格中添加一个新的checkbox,例如,如果checkbox没有包含checkbox。

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 

使用此function来检查范围是否包含checkbox:

 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