【问题标题】:How to output an array of values using a function, "For" loop, and two existing arrays in Excel VBA?如何使用函数、“For”循环和 Excel VBA 中的两个现有数组输出值数组?
【发布时间】:2022-07-15 00:27:34
【问题描述】:

我正在学习入门级工程计算课程。我在使用循环和数组时遇到了问题。

目标是使用单个函数和“For”或“Do”循环一次计算两组 10 个值之间的平方差。

数据/数组分两行,如图:

基本上,我们正在求解单元格 A3 中的 (A1-B1)^2、单元格 B3 中的 (A2-B2)^2 等。

我能够计算出的代码运行但只会显示最后一列数据的正确函数值,因为 (9-1)^2 = 64,如图所示:

Option Base 1
Public Function SqDiff(arrayA As Range, arrayB As Range) As Variant
Dim ncell As Integer
Dim i As Integer
Dim A As Single
Dim B As Single
Dim SquareDifference As Single
For i = 1 To 10 Step 1
    A = arrayA(i)
    B = arrayB(i)
    SquareDifference = (A - B) ^ 2
    SqDiff = SquareDifference
Next i
End Function

【问题讨论】:

    标签: arrays excel vba for-loop


    【解决方案1】:

    由于您将 UDF 用作数组公式,因此您还需要将 SqDiff 作为数组返回:

    Public Function SqDiff(arrayA As Range, arrayB As Range) As Variant
        Dim i As Long
        Dim A As Single
        Dim B As Single
        Dim SquareDifference As Single
    
        'Make sure that the input ranges are of 1 row size and same amount of cells
        If arrayA.Rows.Count = 1 And arrayB.Rows.Count = 1 And arrayA.Cells.Count = arrayB.Cells.Count Then
            'Assign the ranges' value into an array for faster processing
            Dim arrA As Variant
            arrA = arrayA.Value
            
            Dim arrB As Variant
            arrB = arrayB.Value
            
            'Create a temp array of the same size as the input size, to assign to SqDiff later
            Dim output() As Variant
            ReDim output(1 To 1, 1 To UBound(arrA, 2)) As Variant
            
            For i = 1 To UBound(arrA, 2)
                A = arrA(1, i)
                B = arrB(1, i)
                SquareDifference = (A - B) ^ 2
                output(1, i) = SquareDifference
            Next i
            
            SqDiff = output
        End If
    End Function
    

    【讨论】:

    • 太棒了!!!输出函数是我所缺少的,并且弄乱了行和列并忘记使用 ncell。您的解释也非常全面,这很有帮助。谢谢!!
    • @mixxdup 注意 - 我不确定您是否真的需要为此目的制作 UDF,例如根据您的示例,在单元格 A3 中插入​​公式=(A1-A2)^2,然后填写其余单元格....您并不完全需要制作 UDF 并应用数组公式来实现您想要的示例。
    • 这将是一种更简单的方法,但我们的老师告诉我们使用 UDF 和“if”或“do”循环来找到解决方案
    【解决方案2】:

    UDF - 数组公式

    • 如果您没有Office 365,则需要将公式作为数组公式输入
      Ctrl,Shift+Enter。您选择了范围,但只在第一个单元格中输入公式。
    • 请注意,所有三个范围的大小必须相同。

    Option Explicit
    
    Public Function SqDiff( _
        ByVal RangeA As Range, _
        ByVal RangeB As Range) _
    As Double()
    
        Dim rCount As Long: rCount = RangeA.Rows.Count
        If rCount <> RangeB.Rows.Count Then Exit Function
        Dim cCount As Long: cCount = RangeA.Columns.Count
        If cCount <> RangeB.Columns.Count Then Exit Function
    
        Dim aData As Variant, bData As Variant
        If rCount + cCount = 2 Then ' one cell
            ReDim aData(1 To 1, 1 To 1): aData(1, 1) = RangeA.Value
            ReDim bData(1 To 1, 1 To 1): bData(1, 1) = RangeB.Value
        Else ' multiple cells
            aData = RangeA.Value
            bData = RangeB.Value
        End If
        
        Dim Data() As Double: ReDim Data(1 To rCount, 1 To cCount)
        
        Dim r As Long, c As Long
        
        For r = 1 To rCount
            For c = 1 To cCount
                If IsNumeric(aData(r, c)) Then
                    If IsNumeric(bData(r, c)) Then
                        ' Choose/modify the operation.
                        Data(r, c) = (aData(r, c) - bData(r, c)) ^ 2
                    End If
                End If
            Next c
        Next r
            
        SqDiff = Data
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      相关资源
      最近更新 更多