【问题标题】:VBA Adding to ReDim ValueVBA 添加到 ReDim 值
【发布时间】:2020-06-06 08:27:53
【问题描述】:

好的,所以我在使用 ReDim 数组时遇到了问题;有人告诉我这是这样做的方法。我只是不确定将它放在哪里,或者每次通过函数时都必须添加到第一个整数 ((n, n)) 中。仅供参考,“Dim PropArray(0, 5) As String”位于“选项比较数据库”所在代码的最顶部。

Dim PropArray(0, 5) As String

Public Function ShowBeam()

For Each ctl In [Forms]![frmShelfBuilder]
    If Left(ctl.Name, 3) = "Box" Then
        If Int(Right(ctl.Name, (Len(ctl.Name)) - 3)) = Me.txtBeamCount Then
            If Box1.Visible = False Then
            Set rct = ctl
                PropArray(0) = rct.Name
                PropArray(1) = rct.Visible
                PropArray(2) = rct.Height 'Int out
                PropArray(3) = rct.Left 'Int out
                PropArray(4) = rct.Top 'Int out
                PropArray(5) = rct.Width 'Int out
            Else
            Set rct = ctl
            'ReDim Preserve PropArray(n + 1, 5) where "n" is the current number in the row (I think this is were you would add the "ReDim Preserve"?)
                PropArray(0) = rct.Name
                PropArray(1) = rct.Visible
                PropArray(2) = rct.Height 'Int out
                PropArray(3) = rct.Left 'Int out
                PropArray(4) = rct.Top 'Int out
                PropArray(5) = rct.Width 'Int out
            End If
        End If
    End If
Next

我认为我在正确的轨道上。我只是不知道在哪里 ReDim 保留数组以将 1 添加到第一个整数。欢迎任何建议。 Muchacho apreciado!

【问题讨论】:

    标签: arrays vba ms-access multidimensional-array


    【解决方案1】:

    如果您希望能够ReDim 它那么您不能在Dim 中指定大小

    改成这样:

    Dim PropArray() As String
    ReDim PropArray(0, 5)
    

    这将做同样的事情,并允许您稍后ReDim Preserve它。

    注意:ReDim 必须位于例程内部,而不是代码所示的外部。

    比如:

    Dim PropArray() As String
    
    Sub Prop_Init()
      ReDim PropArray(0, 5)
    End Sub
    
    Sub Prop_AddDim()
      ReDim Preserve PropArray(0, 6)
    End Sub
    

    另外请记住,您只能ReDim 数组的最后一部分。

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 2021-05-07
      • 2015-05-06
      • 2015-09-25
      相关资源
      最近更新 更多