【问题标题】:Problem working with variant data type w/vba使用带有 vba 的变体数据类型时出现问题
【发布时间】:2020-12-10 14:22:04
【问题描述】:

我正在尝试学习使用变体数据类型,但遇到了问题。

Public Function z_score(sections As Range, marks As Range) As Variant

    Dim n As Integer
    Dim score() As Variant 'marks range has a few empty cells and error cells as well
                           'hence using variant data type
    
    n = UBound(sections.Value)
    ReDim score(1 To n, 1 To 2)
    score = marks.Value   'assigning marks range values to first column of score

    For i = 1 To n        'adding second column with integer index for calling later
        score(i, 2) = i
    Next i

    z_score = score

End Function

我得到值错误而不是 nx2 矩阵作为输出。 你能帮忙解决这个错误吗? 非常感谢任何帮助,谢谢..

【问题讨论】:

  • 您需要了解 redim 的工作原理。您当前的使用不正确。您的第一个作业“score = Marks.value”也表明您不了解如何在 VBA 中为数组赋值。

标签: arrays vba variant


【解决方案1】:

恐怕有几个方面可能导致此代码失败:

  1. 如果传入的范围只有 1 个单元格,则分配给数组会引发错误。
  2. VBA 没有复制或克隆数组的方法,因此您的代码 score = marks.Value 没有按照您的 cmets 所说的进行,
  3. sections 参数似乎没有做任何事情。您正在根据它调整数组的大小,然后迭代 marks 数组以分配值。
  4. 我不确定你想用这个函数做什么,但如果它是从工作表调用的 UDF,它需要是一个公式数组。

您可以按如下方式调整您的代码,使其更加健壮:

Public Function z_score(marks As Range) As Variant
    Dim scoreArray() As Variant, marksArray() As Variant
    Dim i As Long
    
    marksArray = RangeValueToArray(marks)
    
    ReDim scoreArray(1 To UBound(marksArray, 1), 1 To 2)
    
    For i = 1 To UBound(marksArray, 1)
        scoreArray(i, 1) = i
        scoreArray(i, 2) = marksArray(i, 1)
    Next
    
    z_score = scoreArray
    
    
End Function

Private Function RangeValueToArray(rng As Range) As Variant
    Dim v() As Variant
    
    If rng.Cells.Count = 1 Then
        ReDim v(1 To 1, 1 To 1)
        v(1, 1) = rng.Value2
        RangeValueToArray = v
        Exit Function
    End If
    
    v = rng.Value2
    RangeValueToArray = v
End Function

【讨论】:

  • 安比,谢谢你。 1. Marks 是 n x 1 数组,并且永远是这样 2. 我希望 score = marks.value 的工作方式类似于 set score = marks.valuescore 作为一个变体,即二维数组,它将值放入第一列。我错了,但试图理解为什么。 3. 这将是稍微大一点的程序的一部分,用于计算标记的分段平均值和标准偏差。因此包含部分。 4. 我正在使用 Office 365,幸运的是 Excel 会自动填充到完整的数组中。我知道这有点懒。
  • 是的,在 VBA 中没有复制数组的一维的方法 - 您必须一次迭代并分配一个值。
【解决方案2】:

我想我找到了问题所在。将范围的值分配给数组有效,但使分配的数组 二维数组,但如果给定范围只有一列或一行,则只有 1 列! 因此,将 redim 与 preserve 移动到分配行之后对我有用目的。

但是,如果想要将值分配给 2D 数组中除第一列以外的列(尽管只有 1 列),我目前知道的唯一解决方案是按照 Ambie 建议的方式进行迭代。

Public Function z_score(sections As Range, marks As Range) As Variant

Dim score() As Variant 'marks range has a few empty cells and error cells as well
                     'hence using variant data type
Dim n As Integer

n = UBound(sections.Value)

score = marks.Value   'assigning marks range values to first column of score
ReDim Preserve score(1 To n, 1 To 2)

For i = 1 To n        'adding second column with integer index for calling later
    score(i, 2) = i
Next i

z_score = score

End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 2011-01-27
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2020-07-03
    • 2019-11-20
    相关资源
    最近更新 更多