vba 从某个单元格中删除复选框的宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14965551/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:41:52  来源:igfitidea点击:

Macro to delete a checkbox from a certain cell

excelexcel-vbavba

提问by Jamie Walker

I am pulling check boxes into a spreadsheet to be used to select certain line items to get a final cost. There are a few unneeded check boxes that get pulled in though, probably 5 or so total. I can use macros to get to the specific cells these unneeded check boxes. These unneeded check boxes will not always be in the same place due to my data changing so I will have to delete them one at a time which shouldn't be a problem other than I don't know the code to delete a check box from the active cell. I need a code to delete check box from active cell or selected cell. I have included some of my coding I have tried below. The first section is just getting me to the correct cell to delete the check box out of. The second part are two different codes I have tried to delete the check box but neither worked. I appreciate your input.

我将复选框拉入电子表格,用于选择某些行项目以获得最终成本。有一些不需要的复选框会被拉入,总共可能有 5 个左右。我可以使用宏来访问这些不需要的复选框的特定单元格。由于我的数据发生变化,这些不需要的复选框不会总是在同一个地方,所以我必须一次删除它们,这应该不是问题,除了我不知道从中删除复选框的代码活动单元格。我需要一个代码来删除活动单元格或选定单元格中的复选框。我已经包含了我在下面尝试过的一些编码。第一部分只是让我进入正确的单元格以删除复选框。第二部分是两个不同的代码,我试图删除复选框,但都没有奏效。我感谢您的意见。

'To delete unwanted checkboxes
    Sheets("Quote Sheet").Select
    Range("B9").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(0, -1).Select

    ActiveSheet.Shapes.Range(Array("Check Box 456")).Select
    Selection.Delete
    Selection.Cut

    ActiveCell.CheckBoxes.Delete
    Selection.FormatConditions.Delete

回答by Peter Albert

This code will delete any Excel checkbox located at the active cell.

此代码将删除位于活动单元格的任何 Excel 复选框。

Sub DeleteCheckbox()
    Dim cb As CheckBox

    For Each cb In ActiveSheet.CheckBoxes
        If cb.TopLeftCell.Address = ActiveCell.Address Then cb.Delete
    Next
End Sub

In case you're using ActiveX checkboxes, this code will do the job:

如果您使用 ActiveX 复选框,此代码将完成这项工作:

Sub DeleteActiveXCheckbox()
    Dim obj As OLEObject
    Dim cb As MSForms.CheckBox

    For Each obj In ActiveSheet.OLEObjects
        If TypeOf obj.Object Is MSForms.CheckBox Then
            Set cb = obj.Object
            If cb.ShapeRange.Item(1).TopLeftCell.Address = _
                ActiveCell.Address Then obj.Delete
        End If
    Next
End Sub