我是编程新手(所以请耐心等待)并且正在尝试开发涉及用户形式复选框的代码。我想填充每个月使用复选框的列表。在新月期间,用户将单击宏按钮以插入新月。但是,我需要复选框来重置新月份,同时保持前几个月完好无损。我知道如何插入新月的列,但我无法弄清楚如何重置复选框。有什么建议?我的代码如下:
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
答案 0 :(得分:0)
这取决于您的复选框与一列宽和一行高对齐。它还假设您正在复制整个列。我确信它不会完美地运作,但这是一个开始,我希望:
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
编辑:这会在左侧插入新行。它更简单,因为不需要计算最后一列:
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
答案 1 :(得分:0)
如果你准备好了CheckBoxes(适当地命名),那么你可以轻松地清除/勾选复选框。如果您添加引用" 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