在 VBA 中的 sub 之间传递信息
Passing information between sub in VBA
我正在尝试将一个数组从一个子单元转移到另一个子单元,以便它可以用于计算。计算完成后,我需要将它们发送回第一个 sub。这是我的代码,当我 运行 时,它绝对没有任何反应。
Public Sub SampleStats()
Option Explicit
Dim n As Integer ' sample size
Dim x() As Double ' array containing x values
Dim i As Integer ' loop counter
Dim rang As Double
' variables to hold output from computation subroutines
Dim minVal As Double ' minimim value
Dim maxVal As Double ' maximum value
Dim mean As Double ' mean value
Dim var As Double ' variance
Dim stdDev As Double ' standard deviation
' get sample size
Sheets("Q1_Stats").Select
Range("B8").Select
n = ActiveCell.Value
' create an array
ReDim x(n - 1) ' redimension x now that we know how many values we have
' get x values
Range("B11").Select
For i = 0 To n - 1
x(i) = ActiveCell.Offset(i)
Next i
' Call subroutine to compute statistics
' *** put your call statemenAst below (with arguments)
Call ComputeStatistics(x(), n)
' Call ComputeStatistics(ByVal ..., ByRef ..., ... )
' now output results
Range("F9").Select
ActiveCell.Offset(0).Value = minVal
Range("F10").Select
ActiveCell.Offset(0).Value = maxVal
Range("F11").Select
ActiveCell.Offset(0).Value = rang
Range("F12").Select
ActiveCell.Offset(0).Value = mean
Range("F13").Select
ActiveCell.Offset(0).Value = var
Range("F14").Select
ActiveCell.Offset(0).Value = stdDev
End Sub
Sub ComputeStatistics(x() As Double, n As Integer)
Dim rang As Double
Dim maxVal As Single
Dim i As Integer
Dim mean As Single
Dim minVal As Double
i = 0
maxVal = x(0)
For i = 1 To UBound(x)
If maxVal < x(i) Then
maxVal = x(i)
End If
Next i
'Computes mean average
i = 0
mean = x(0)
For i = 1 To UBound(x)
mean = mean + x(i)
Next i
mean = mean / n
'Computes the lowest value
i = 0
minVal = x(0)
For i = 1 To UBound(x)
If minVal > x(i) Then
minVal = x(i)
End If
Next i
'Calulates Range
rang = maxVal - minVal
end sub
问题是您在两个过程中使用相同的 name 声明了局部变量,并且期望它们是相同的变量。例如,您在 SampleStats
中使用变量 minVal
,但您从未给它赋值。您在 ComputeStatistics
中声明了一个具有相同名称的单独变量这一事实是无关紧要的。
将您的代码合并到一个过程中,因为它无论如何都是一个逻辑块。这样你就不用担心来回传递值了。
如果你仍然想分解出 ComputeStatistics
部分,一旦你开始工作,请传入你想要更新的所有变量(例如 minVal、maxVal、rang 等),不要在 ComputeStatistics
过程中声明它们。
我正在尝试将一个数组从一个子单元转移到另一个子单元,以便它可以用于计算。计算完成后,我需要将它们发送回第一个 sub。这是我的代码,当我 运行 时,它绝对没有任何反应。
Public Sub SampleStats()
Option Explicit
Dim n As Integer ' sample size
Dim x() As Double ' array containing x values
Dim i As Integer ' loop counter
Dim rang As Double
' variables to hold output from computation subroutines
Dim minVal As Double ' minimim value
Dim maxVal As Double ' maximum value
Dim mean As Double ' mean value
Dim var As Double ' variance
Dim stdDev As Double ' standard deviation
' get sample size
Sheets("Q1_Stats").Select
Range("B8").Select
n = ActiveCell.Value
' create an array
ReDim x(n - 1) ' redimension x now that we know how many values we have
' get x values
Range("B11").Select
For i = 0 To n - 1
x(i) = ActiveCell.Offset(i)
Next i
' Call subroutine to compute statistics
' *** put your call statemenAst below (with arguments)
Call ComputeStatistics(x(), n)
' Call ComputeStatistics(ByVal ..., ByRef ..., ... )
' now output results
Range("F9").Select
ActiveCell.Offset(0).Value = minVal
Range("F10").Select
ActiveCell.Offset(0).Value = maxVal
Range("F11").Select
ActiveCell.Offset(0).Value = rang
Range("F12").Select
ActiveCell.Offset(0).Value = mean
Range("F13").Select
ActiveCell.Offset(0).Value = var
Range("F14").Select
ActiveCell.Offset(0).Value = stdDev
End Sub
Sub ComputeStatistics(x() As Double, n As Integer)
Dim rang As Double
Dim maxVal As Single
Dim i As Integer
Dim mean As Single
Dim minVal As Double
i = 0
maxVal = x(0)
For i = 1 To UBound(x)
If maxVal < x(i) Then
maxVal = x(i)
End If
Next i
'Computes mean average
i = 0
mean = x(0)
For i = 1 To UBound(x)
mean = mean + x(i)
Next i
mean = mean / n
'Computes the lowest value
i = 0
minVal = x(0)
For i = 1 To UBound(x)
If minVal > x(i) Then
minVal = x(i)
End If
Next i
'Calulates Range
rang = maxVal - minVal
end sub
问题是您在两个过程中使用相同的 name 声明了局部变量,并且期望它们是相同的变量。例如,您在 SampleStats
中使用变量 minVal
,但您从未给它赋值。您在 ComputeStatistics
中声明了一个具有相同名称的单独变量这一事实是无关紧要的。
将您的代码合并到一个过程中,因为它无论如何都是一个逻辑块。这样你就不用担心来回传递值了。
如果你仍然想分解出 ComputeStatistics
部分,一旦你开始工作,请传入你想要更新的所有变量(例如 minVal、maxVal、rang 等),不要在 ComputeStatistics
过程中声明它们。