Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何将两个命名复选框添加到一个单元格_Vba_Excel - Fatal编程技术网

Vba 如何将两个命名复选框添加到一个单元格

Vba 如何将两个命名复选框添加到一个单元格,vba,excel,Vba,Excel,我环顾四周,但不知道如何在一个单元格中添加两个复选框。是否需要使用OLEObjects.Add?我需要在E列中添加两列复选框。提前谢谢 以下是我的尝试: LRow = ActiveSheet.Range("G" & Rows.count).End(xlUp).Row For cell = 2 To LRow If Cells(cell, "G").Value <> "" Then CLeft = Cells(cell, "E").Left

我环顾四周,但不知道如何在一个单元格中添加两个复选框。是否需要使用OLEObjects.Add?我需要在E列中添加两列复选框。提前谢谢

以下是我的尝试:

LRow = ActiveSheet.Range("G" & Rows.count).End(xlUp).Row

For cell = 2 To LRow

    If Cells(cell, "G").Value <> "" Then

        CLeft = Cells(cell, "E").Left

        CTop = Cells(cell, "E").Top

        CHeight = Cells(cell, "E").Height / 2

        CWidth = Cells(cell, "E").Width


        ActiveSheet.CheckBoxes.Add(CLeft, CTop, CWidth, CHeight).Select

        With Selection
            .Caption = ""
            .name = "cbSet1_" & cell
            .Value = xlOff
            .LinkedCell = "E" & cell
            .Display3DShading = False

        End With
    End If
Next cell

For cellF = 2 To LRow

    If Cells(cellF, "G").Value <> "" Then

        CLeftF = Cells(cellF, "E").Left

        CTopF = Cells(cellF, "E").Top

        CHeightF = Cells(cellF, "E").Height

        CWidthF = Cells(cellF, "E").Width


        ActiveSheet.CheckBoxes.Add(CLeftF, CTopF, CWidthF, CHeightF).Select

        With Selection
            .Caption = ""
            .name = "cbSet2_" & cell
            .Value = xlOff
            .Display3DShading = False
        End With
    End If
Next cellF

我对你的代码做了一些小改动。我们不需要两个回路

如果不行,请告诉我。试试这个

LRow = ActiveSheet.Range("G" & Rows.count).End(xlUp).row

For cell = 2 To LRow

    If Cells(cell, "G").Value <> "" Then

        'Getting left, top, height and width
        CLeft = Cells(cell, "E").Left

        CTop = Cells(cell, "E").Top

        CHeight = Cells(cell, "E").Height / 2

        CWidth = Cells(cell, "E").Width / 2

        'Add first check box
        ActiveSheet.CheckBoxes.Add(CLeft, CTop, CWidth, CHeight).Select

        With Selection
            .Caption = ""
            .Name = "cbSet1_" & cell
            .Value = xlOff
            .LinkedCell = "E" & cell
            .Display3DShading = False

        End With

        'Add second check box
        ActiveSheet.CheckBoxes.Add(CLeft + CWidth, CTop, CWidth, CHeight).Select

        With Selection
            .Caption = ""
            .Name = "cbSet2_" & cell
            .Value = xlOff
            .Display3DShading = False
        End With

    End If

Next cell

是的,谢谢。我看到了复选框。我想更改复选框的名称。我该怎么做?另外,如果我使用OLEObjects.Add,会有什么不同?您希望如何验证名称?您可以通过单击复选框在工作表中看到它,它将在左上角的框中显示其名称。我认为主要的不同是代码风格。