防止复制控件的填充?

时间:2014-12-24 14:37:18

标签: excel excel-vba vba

我有一个excel电子表格,其中有许多复选框链接到复选框之间的工作表中的单元格。

然后我根据复选框的条件和其他一些数据进行格式化。

现在我需要更改格式或公式,以便更改一些单元格然后填充以更改其余单元格。

问题是fill会复制所有范围内的复选框,以便第一个复选框出现在所有单元格中。 (复制和粘贴在没有复选框的情况下可以正常工作,但由于此剪贴板或其他电子表格不断重置剪贴板,因此变得很困难。)

是否有任何方法可以阻止此行为并将所有复选框保留在填充时的位置。到目前为止,没有任何填充选项正常工作。

另一种对我有用的解决方案,

复选框都是使用vba宏创建的,它将它们放在一个单元格中并链接到工作表中的正确单元格。 如果我可以先删除它们,我可以轻松地重新创建复选框。

我知道CheckBoxes.Delete但是我无法在工作表上使用它,因为工作表中还有其他复选框我无法删除。 有没有办法只删除特定范围内的所有复选框?

这将允许我根据需要进行格式化,删除所有复选框并重新创建它们。

1 个答案:

答案 0 :(得分:0)

这是您的备用解决方案。如果你是在动态创建这些,那么给他们一个名字,以区别于其他人。然后你可以遍历每个,检查名称并删除

Sub RemoveCheckBoxes()
Dim checkBoxKeyWord As String
'Lets say this is what each checkbax name starts with
checkBoxKeyWord = "cbRemovable"

Dim shp As Shape
'loop through the sheet and check the shapes (Form controls are shapes)
For Each shp In Sheets(1).Shapes
    'see if the name matches
    If Left(shp.Name, Len(checkBoxKeyWord)) = checkBoxKeyWord Then
        'delete the checkbox
        shp.Delete
    End If

Next

End Sub

这是一个检查范围的版本

Sub RemoveCheckBoxes()
Dim checkBoxKeyWord As String
Dim r As Range
Set r = Sheets(1).Columns("B")
'Lets say this is what each checkbax name starts with
checkBoxKeyWord = "cbRemovable"

Dim shp As Shape
'loop through the sheet and check the shapes (Form controls are shapes)
For Each shp In Sheets(1).Shapes
    'see if the name matches
    If Left(shp.Name, Len(checkBoxKeyWord)) = checkBoxKeyWord Then
        Dim cbRange As Range
        Set cbRange = Sheets(1).Range(shp.ControlFormat.LinkedCell)
        If Not Application.Intersect(cbRange, r) Is Nothing Then
            'delete the checkbox as its linked cell is in the range
            shp.Delete
        End If
    End If
Next

End Sub