在特定的单元格中创buildActiveXcheckbox

在我的工作表1中,列A有一些值,我需要为特定单元格中的工作表2中的所有值创build一个Active Xcheckbox。 首先,我需要检查Active Xcheckbox是否存在值,如果不存在,我需要创build。 我已经尝试了下面的代码,但是它创build了重复的checkbox。

Sub Addcheckbox() Dim rng As Range, cell As Range Dim rr As Integer Dim tf As Boolean Dim shpTemp As Shape Set rng = Range("A1:A8") Set Destrng = Range("A2:A9") rr = 2 For Each cell In Worksheets("Sheet1").Range("A1:A8") If Not IsEmpty(cell.Value) Then With ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1", _ Left:=51.75, Top:=183, Width:=120, Height:=19.5) .Object.Caption = cell.Value End With End If rr = rr + 1 Next cell End Sub 

如何检查ActiveXcheckbox是否已经存在于工作表中或者没有标题名称

我试过这个代码检查checkbox..但它不工作..

 Function shapeExists(ByRef shapename As String) As Boolean shapeExists = False Dim sh As Shape For Each sh In ActiveSheet.Shapes If sh.name = shapename Then shapeExists = True Exit Function End If Next sh End Function 

ActiveXcheckbox是OleObjects 。 这是你正在尝试?

你也需要指定正确的。否则他们将在同一个地方创build。 看看我如何使用Top:=cell.Top

 Sub Sample() Dim rng As Range, cell As Range Dim rr As Integer Dim tf As Boolean Dim shpTemp As Shape Set rng = Range("A1:A8") Set Destrng = Range("A2:A9") rr = 2 For Each cell In Worksheets("Sheet1").Range("A1:A8") If Not IsEmpty(cell.Value) Then If Not CBExists(cell.Value) Then '<~~ Check if the checkbox exists With ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1", _ Left:=51.75, Top:=cell.Top, Width:=120, Height:=19.5) .Object.Caption = cell.Value End With End If End If rr = rr + 1 Next cell End Sub '~~> Function to check if the checkbox exists Function CBExists(s As String) As Boolean Dim oleObj As OLEObject Dim i As Long For i = 1 To Worksheets("Sheet1").OLEObjects.Count If s = Worksheets("Sheet1").OLEObjects(i).Object.Caption Then CBExists = True Exit Function End If Next i End Function