【问题标题】:Creating an array of checkbox names创建一个复选框名称数组
【发布时间】:2013-06-13 15:15:33
【问题描述】:

我在创建一组 activex 复选框名称时遇到了一些问题。我想创建这个数组,这样我就可以使用 For 循环,而不必分别输入每个复选框代码。这是我的代码的一部分。我得到的错误说类型不匹配并突出显示 &。我想要数组(0)=ThirteenJan,数组(1)=ThirteenFeb等等。

Dim Month(0 To 11) As String
Dim Year(0 To 3) As String
Dim Time(0 To 47) As CheckBox
Dim i, j, k, l, m As Integer

'月份的初始值是这样命名的,因为这就是按钮的命名方式 '初始值 月(0)=“一月” 月(1)=“二月” 月(2)=“三月” 月(3)=“四月” 月(4)=“五月” 月(5)=“六月” 月(6)=“七月” 月(7)=“八月” 月(8)=“九月” 月(9)=“十月” 月(10)=“十一月” Month(11) = "十二月"

Year(0) = "Thirteen"
Year(1) = "Fourteen"
Year(2) = "Fifteen"
Year(3) = "Sixteen"

k = 0

'I can't get the following code to work and I'm not sure what's wrong with it. It says     type mismatch and highlights the &.
'Create an array that has all the names of the checkboxes in each element of it
For i = 0 To 3
    For j = 0 To 11
        Set Time(k) = Year(i) & Month(j)
    k = k + 1
    Next j
    k = k + 1
Next i

k = 4
l = 18

For i = LBound(Time) To UBound(Time)
    'j loops through worksheets, the summary sheets are organized differently than the     rest of the workbook so they have to have their own code
    For j = 2 To 3
        'k loops through the columns, 54 is Column BB
        If k = 16 Or k = 29 Or k = 42 Then
        k = k + 1
        End If
        If Time(i).Value = True Then
            Sheets(j).Columns(k).EntireColumn.Hidden = False
        Else
            Sheets(j).Columns(k).EntireColumn.Hidden = True
        End If
    Next j
    For j = 4 To 9
        If l = 30 Or l = 31 Or l = 44 Or l = 45 Or l = 58 Or l = 59 Then
            l = l + 1
        End If

【问题讨论】:

  • Month() 是一个内置函数。您可以尝试将数组重命名为Months() 吗? (和Year()Years()。)
  • 和你声明的Time variable一样的评论——把它改成Times或其他。
  • 非常感谢大家抓到这些,我的代码仍然无法正常工作,但这些肯定是一些错误。我现在得到的错误是它说类型不匹配并突出显示 &。设置时间(k) = 年(i) & 月(j)

标签: arrays excel checkbox activex vba


【解决方案1】:
Sub Macro1()
        Dim MyCheckboxes(1 To 10) As OLEObject
        Dim shp As Shape
        Range("A1").Select
        For i = 1 To 10
            ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1", Link:=False, _
                DisplayAsIcon:=False, Left:=240, Top:=75.75, Width:=49.5, Height:= _
                17.25).Select
            Set MyCheckboxes(i) = Selection
            Set shp = ActiveSheet.Shapes("CheckBox" & i)
            shp.Left = ActiveCell.Left
            shp.Top = ActiveCell.Top
            shp.Height = ActiveCell.Height
            shp.Width = ActiveCell.Width
            ActiveCell.Offset(2, 0).Select
        Next
    End Sub

【讨论】:

  • 嘿@Gary 的学生感谢您帮助我,但您能否解释一下您的代码是做什么的,请我对 vba 的了解有限
  • 在循环中我输入了一个复选框;将新复选框存储在数组中;通过将其视为形状来调整复选框的大小和位置
猜你喜欢
  • 1970-01-01
  • 2011-08-14
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 2019-03-10
  • 1970-01-01
  • 2018-11-11
相关资源
最近更新 更多