在VBA里总会有dim mat 这个mat是什么意思 谢谢大家!
比如FunctionSampleCovariance(Rng_Name1AsString,Rng_Name2AsString)AsDouble'Tocallthisfun...
比如
FunctionSampleCovariance(Rng_Name1 As String, Rng_Name2 As String) As Double
'To call this function, in the spreadshhet type:=SampleCovariance("RangeName1","RangeName1") and then pressthe Enter key.
Dim Rng1 As Range, Rng2 As Range
Dim i As Integer, j As Integer
Dim db As Double, db1 As Double, db2 AsDouble
Dim mat1(), mat2()
Set Rng1 = Range(Rng_Name1)
Set Rng2 = Range(Rng_Name2)
mat1 = Rng1
mat2 = Rng2
If UBound(mat1, 1) <> UBound(mat2, 1)Then
MsgBox "In SampleCovariance,number of rows included in the two inputted named ranges are not thesame.", , "Error Message"
End If
If UBound(mat1, 2) <> UBound(mat2, 2)Then
MsgBox "In SampleCovariance,number of columns included in the two inputted named ranges are not thesame.", , "Error Message"
End If
db1 = SampleMean(Rng_Name1)
db2 = SampleMean(Rng_Name2)
db = 0
For i = 1 To UBound(mat1, 1)
For j = 1 To UBound(mat1, 2)
db = db + (mat1(i, j) - db1) *(mat2(i, j) - db2)
Next j
Next i
SampleCovariance = db / (UBound(mat1, 1) *UBound(mat1, 2) - 1)
EndFunction 展开
FunctionSampleCovariance(Rng_Name1 As String, Rng_Name2 As String) As Double
'To call this function, in the spreadshhet type:=SampleCovariance("RangeName1","RangeName1") and then pressthe Enter key.
Dim Rng1 As Range, Rng2 As Range
Dim i As Integer, j As Integer
Dim db As Double, db1 As Double, db2 AsDouble
Dim mat1(), mat2()
Set Rng1 = Range(Rng_Name1)
Set Rng2 = Range(Rng_Name2)
mat1 = Rng1
mat2 = Rng2
If UBound(mat1, 1) <> UBound(mat2, 1)Then
MsgBox "In SampleCovariance,number of rows included in the two inputted named ranges are not thesame.", , "Error Message"
End If
If UBound(mat1, 2) <> UBound(mat2, 2)Then
MsgBox "In SampleCovariance,number of columns included in the two inputted named ranges are not thesame.", , "Error Message"
End If
db1 = SampleMean(Rng_Name1)
db2 = SampleMean(Rng_Name2)
db = 0
For i = 1 To UBound(mat1, 1)
For j = 1 To UBound(mat1, 2)
db = db + (mat1(i, j) - db1) *(mat2(i, j) - db2)
Next j
Next i
SampleCovariance = db / (UBound(mat1, 1) *UBound(mat1, 2) - 1)
EndFunction 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询