通过VBA宏将复选框插入特定单元格 [英] insert check box to a particular cell through vba macro

查看:133
本文介绍了通过VBA宏将复选框插入特定单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过宏在特定单元格中插入复选框.例如:单击命令按钮后,我应该能够将复选框添加到 A1 单元格.

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 cell.

Sheets("Pipeline Products").Range("O" & i & ":AG" & i).Select 
ActiveSheet.CheckBoxes.Add(4, 14.5, 72, 17.25).Select 

With Selection 
    .Caption = "" 
    .Value = xlOff '
    .LinkedCell = "C" & ToRow     
    .Display3DShading = False 
End With 

推荐答案

此简单行允许您将CheckBox添加到单元格A1中,并相应地设置宽度和高度:

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

您可以通过以下方式轻松将其添加到CommandButton:

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

根据需要更改此代码.

这篇关于通过VBA宏将复选框插入特定单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆