【问题标题】:How to declare and fill a two dimensional array with a function vba如何用函数vba声明和填充二维数组
【发布时间】:2018-02-15 09:50:18
【问题描述】:

我有一个在子例程中声明的数组。我希望它充满两个值。一个用于单元格列表的每个坐标。我知道这并不总是最好的方法,但是由于涉及我程序的其余部分的原因,我需要完全这样做。

'This sub contains more, I've just included only the relevant stuff
    Sub Start_Click()
        Dim arTime() As Integer
        arTime = FillTimeArray(arTime)
    End Sub


'This is used to fill the array with a list of 12 columns that are 24 rows long
'They are offsetted intentionally

    Function FillSpotArray(ByRef arr() As Integer) As Integer()
        For j = 0 To 11
            For i = 0 To 23
                arr(i, j) = Format(i + 2, j + 1)
            Next i
        Next j
    End Function

【问题讨论】:

  • 您的函数名为 FillSpotArray,但在您的测试 Sub 中,您调用的是 FillTimeArray...
  • 我希望它填充两个值。一个用于单元格列表的每个坐标这是否意味着恰好有 2 个值,或者每个坐标有 2 个值?此外,Format(i + 2, j + 1) 只能是错误的 => Format(0 + 2, 0 + 1) = Format(2, 1)1 不是有效格式...请确保您知道 Format() 在做什么。 (您可能正在寻找Array(i + 2, j + 1)...)

标签: arrays excel vba function


【解决方案1】:

您还没有确定数组的尺寸:

Sub Start_Click()
    Dim arTime(23, 11) As Integer
    arTime = FillTimeArray(arTime)
End Sub

【讨论】:

    【解决方案2】:

    你需要对数组进行Dim,正确调用函数,并在函数中正确返回输出

    Sub Start_Click()
        Dim arTime As Variant
        arTime = FillSpotArray()
    End Sub
    
    
    'This is used to fill the array with a list of 12 columns that are 24 rows long
    'They are offsetted intentionally
    
    Function FillSpotArray() As Integer()
        Dim arr(23, 11) As Integer
        For j = 0 To 11
            For i = 0 To 23
                arr(i, j) = Format(i + 2, j + 1)
            Next i
        Next j
        FillSpotArray = arr
    End Function
    

    我不完全确定你在这里使用Format

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 2018-01-27
      • 2016-05-27
      • 1970-01-01
      相关资源
      最近更新 更多