如何使用函数、"For" 循环和 Excel VBA 中的两个现有数组输出值数组?
How to output an array of values using a function, "For" loop, and two existing arrays in Excel VBA?
我对使用 VBA 编码还很陌生。我在非常入门级的工程计算课程中使用 Excel 的 VBA,但我发现自己陷入了使用循环和数组的问题。我会尽量详细的。
目标是使用单个函数和“For”或“Do”循环一次计算两组 10 个值之间的平方差。我认为“For-Next”循环效果最好。预先存在的数据集/数组分两行,如图:
本质上,我们正在求解单元格 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
我确定这是一个我没有发现的非常简单的错误。无论哪种方式,我都感谢任何输入。谢谢!
由于您将 UDF 用作数组公式,因此您还需要 return 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
UDF - 数组公式
- 如果您没有 Office 365,则需要将公式作为数组公式输入
Ctrl,Shift+Enter。您 select 范围,但只在第一个单元格中输入公式。
- 请注意,所有三个范围的大小必须相同。
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
我对使用 VBA 编码还很陌生。我在非常入门级的工程计算课程中使用 Excel 的 VBA,但我发现自己陷入了使用循环和数组的问题。我会尽量详细的。
目标是使用单个函数和“For”或“Do”循环一次计算两组 10 个值之间的平方差。我认为“For-Next”循环效果最好。预先存在的数据集/数组分两行,如图:
本质上,我们正在求解单元格 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
我确定这是一个我没有发现的非常简单的错误。无论哪种方式,我都感谢任何输入。谢谢!
由于您将 UDF 用作数组公式,因此您还需要 return 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
UDF - 数组公式
- 如果您没有 Office 365,则需要将公式作为数组公式输入
Ctrl,Shift+Enter。您 select 范围,但只在第一个单元格中输入公式。 - 请注意,所有三个范围的大小必须相同。
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