首页 > 解决方案 > VBA 添加到 ReDim 值

问题描述

好的,所以我在使用 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!

标签: arraysvbams-accessmultidimensional-array

解决方案


如果你想能够做到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访问数组的最后一部分。


推荐阅读