【问题标题】:Clear Userform Checkboxes after CopyPaste VBACopyPaste VBA 后清除用户表单复选框
【发布时间】:2015-07-16 12:13:36
【问题描述】:

我是编程新手(所以请耐心等待)并且正在尝试开发涉及用户表单复选框的代码。我想填充每个月使用复选框的列表。在新月份期间,用户将单击宏按钮以插入新月份。但是,我需要为新月份重置复选框,同时保持前几个月的完整性。我了解如何插入新月份的列,但我不知道如何重置复选框。有什么建议么?我的代码如下:

Sub new_month()

Range("B:B").Select
Range("B3:B16").Copy
Selection.Insert Shift:=xlToRight
Range("b3: b16 ").Select
Selection.CheckBoxes.Value = 0

End Sub

【问题讨论】:

    标签: excel vba checkbox


    【解决方案1】:

    如果您已经很好地准备了复选框(适当地命名它们),那么您可以轻松地清除/勾选复选框。如果您将引用“Microsoft Forms 2.0 对象库”添加到 VBE,您可以看到可以检查/更改的属性。

    下面的这个宏将列出复选框的一些属性,包括名称、文本和值的状态:

    Option Explicit
    
    Sub ClearCheckBoxes()
        Dim oWS As Worksheet, oCB As CheckBox
        Set oWS = ActiveSheet
        For Each oCB In oWS.CheckBoxes
            With oCB
                Debug.Print "=== CheckBox Properties ==="
                Debug.Print "Name: " & .Name
                Debug.Print "Text: " & .Text
                Debug.Print "Value: " & .Value
                Debug.Print "TopLeftCell: " & .TopLeftCell.Address
                Debug.Print "BottomRightCell: " & .BottomRightCell.Address
                Debug.Print "Top: " & .Top
                Debug.Print "TopLeftCell.Top: " & .TopLeftCell.Top
                Debug.Print "Left: " & .Left
                Debug.Print "Height: " & .Height
                Debug.Print "Width: " & .Width
                Debug.Print "LinkedCell: " & .LinkedCell
                Debug.Print "ShapeRange.Name: " & .ShapeRange.Name ' <-- You can use Shapes collection from Worksheet to reference
            End With
            'oCB.Value = False ' Clears the checkbox
            'oCB.Value = True ' Ticks the checkbox
            Debug.Print ""
        Next
    End Sub
    

    【讨论】:

    • 感谢您的意见!
    【解决方案2】:

    这取决于您的复选框与一列宽和一行高对齐。它还假设您正在复制整个列。我确信它不会完美运行,但我希望这是一个开始:

    Sub new_month()
    Dim ws As Excel.Worksheet
    Dim shp As Shape
    Dim LastCol As Long
    
    Set ws = ActiveSheet
    With ws
        LastCol = .Cells(3, .Columns.Count).End(xlToLeft).Column
        .Columns(LastCol).Copy
        .Columns(LastCol).Insert Shift:=xlToRight
        For Each shp In ws.Shapes
            If shp.TopLeftCell.Column = LastCol + 1 Then
                shp.ControlFormat.Value = False
            End If
        Next shp
    End With
    End Sub
    

    EDIT:这会在左侧插入新行。它更简单,因为不需要计算最后一列:

    Sub new_month()
    Dim ws As Excel.Worksheet
    Dim shp As Shape
    
    Set ws = ActiveSheet
    With ws
        .Columns(1).Copy
        .Columns(1).Insert Shift:=xlToRight
        For Each shp In ws.Shapes
            If shp.TopLeftCell.Column = 1 Then
                shp.ControlFormat.Value = False
            End If
        Next shp
    End With
    End Sub
    

    【讨论】:

    • 非常感谢!这几乎完全按照我想要的方式工作,但是我怎样才能将最新的列插入到旧数据的左侧?即四月...三月...二月...一月
    猜你喜欢
    • 2020-10-28
    • 1970-01-01
    • 2017-04-03
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多