【问题标题】:Excel 2003, adding content with checkboxesExcel 2003,使用复选框添加内容
【发布时间】:2016-11-19 04:15:49
【问题描述】:

我正在使用 VBA 用行来扩展一个范围,每一行都有自己的复选框。

到目前为止,代码如下所示:

    Dim objColumnHeadings As Range, objDBsheet As Worksheet
    Dim lngRow As Long, objCell As Range
    Dim objCheckbox As Object
    Set objDBsheet = getDBsheet()
    Set objColumnHeadings = objDBsheet.Range("ColumnHeadings")
    objColumnHeadings.ClearContents
    lngRow = 1
    For Each varExisting In objColumns
        objColumnHeadings.Cells(lngRow, 1).Value = varExisting
        Set objCell = objColumnHeadings.Cells(lngRow, 2)
        Set objCheckbox = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1" _
                            , Left:=412.8 _
                            , Top:=objCell.Top _
                            , Height:=10 _
                            , Width:=9.6)
        objCheckbox.Name = "cb" & lngRow
        objCheckbox.Appearance.Caption = ""
        objCheckbox.Appearance.BackColor = &H808080
        objCheckbox.Appearance.BackStyle = 0
        lngRow = lngRow + 1
        If lngRow > 1 Then
            Exit For
        End If
    Next

设置复选框的名称有效,但设置其他属性无效,并导致运行时错误:'438',Object 不支持此属性或方法。

当我查看新添加的复选框的属性时,名称设置正确,但未设置标题、背景颜色和背景样式。

如何以编程方式设置这些?

【问题讨论】:

    标签: excel vba checkbox


    【解决方案1】:

    使用MSForms.CheckBox,并在其中设置Object,这样更容易。使用以下示例代码。

    Sub test()
    
        Dim objCheckbox As MSForms.CheckBox
    
         Set objCheckbox = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1" _
                                , Left:=10.8 _
                                , Top:=10 _
                                , Height:=25 _
                                , Width:=200).Object
            objCheckbox.Name = "Dummy_Test"
            objCheckbox.Caption = "Test"
            objCheckbox.BackColor = vbRed
            objCheckbox.BackStyle = 0
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多