insert check box to a particular cell through vba macro

后端 未结 3 1463
没有蜡笔的小新
没有蜡笔的小新 2021-01-16 09:26

I would like to insert the check box in particular cell through macro. For example: On click of a command button i should be able to add the check box to A1 cel

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-16 09:36

    This simple line allows you to add CheckBox to cell A1 and set width and height accordingly:

    ActiveSheet.OLEObjects.Add "Forms.CheckBox.1", Left:=Range("A1").Left, Top:=Range("A1").Top, Width:=Range("A1").Width, Height:=Range("A1").Height
    

    You can easily add it to CommandButton this way:

    Private Sub CommandButton1_Click()
    
        ActiveSheet.OLEObjects.Add "Forms.CheckBox.1", Left:=Range("A1").Left, Top:=Range("A1").Top, Width:=Range("A1").Width, Height:=Range("A1").Height
    
    End Sub
    

    Edit Your code improved...

    You simply need to add loop to insert checkboxes into several cells:

    Sub YourCode_Improvment()
    
        Dim i
        '
        For i = 1 To 10 'cells from 1st to 10th
    
        ActiveSheet.CheckBoxes.Add(Cells(i, "A").Left, _
                                    Cells(i, "A").Top, _
                                    72, 17.25).Select
        With Selection
            .Caption = ""
            .Value = xlOff '
            .LinkedCell = "C" & i
            .Display3DShading = False
        End With
        Next
    End Sub
    

    Change this code accordingly, if needed.

提交回复
热议问题