通过 vba 宏将复选框插入特定单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16992195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
insert check box to a particular cell through vba macro
提问by user2457968
I would like to insert the check box in particular cell through macro. Kindly advise on how to go about.
我想通过宏在特定单元格中插入复选框。请建议如何去做。
For Example: On click of a command button i should be able to add the check box to A1
cell.
例如:单击命令按钮时,我应该能够将复选框添加到A1
单元格中。
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
Thank you, Prashanth
谢谢你,普拉尚
回答by Kazimierz Jawor
This simple line allows you to add CheckBox to cell A1 and set width and height accordingly:
这个简单的行允许您将 CheckBox 添加到单元格 A1 并相应地设置宽度和高度:
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:
您可以通过以下方式轻松将其添加到 CommandButton:
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
EditYour 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.
如果需要,相应地更改此代码。
回答by raybiss
You can use a For Each loop to add the check boxes.
您可以使用 For Each 循环来添加复选框。
Dim i as Integer
Dim cel As Range
i = 10
For Each cel In Sheets("Pipeline Products").Range("O" & i & ":AG" & i)
ActiveSheet.OLEObjects.Add "Forms.CheckBox.1", Left:=cel.Left, Top:=cel.Top, Width:=cel.Width, Height:=cel.Height
Next
Hope this helps.
希望这可以帮助。
回答by Suobig
Slightly upgraded code in the top comment. Simply select a range and run it, it'll fill all selected cells with checkboxes:
顶部评论中的代码略有升级。只需选择一个范围并运行它,它就会用复选框填充所有选定的单元格:
Sub InsertCheckboxes()
Dim c As Range
For Each c In Selection
Dim cb As CheckBox
Set cb = ActiveSheet.CheckBoxes.Add(c.Left, _
c.Top, _
c.Width, _
c.Height)
With cb
.Caption = ""
.Value = xlOff
.LinkedCell = c.Address
.Display3DShading = False
End With
Next
End Sub