Excel VBA使用范围作为输入
我正在学习Excel VBA,并试图创建一个简单的函数 我的想法是,我将使用一个单元格作为输入,函数将告诉您放置在该单元格周围的12个值的标准偏差 如果我输入getstd(A1),它将得到A1,A3,A5,A7,C1,C3,C5,C7,E1,E3,E5和E7的标准偏差 如果我输入getstd(X23),它将获得12个其他值的std,这些值位于X23周围的相同偏移处 我现在最大的问题是弄清楚如何使用细胞作为输入 例如,在尝试偏移时:Excel VBA使用范围作为输入,excel,vba,input,Excel,Vba,Input,我正在学习Excel VBA,并试图创建一个简单的函数 我的想法是,我将使用一个单元格作为输入,函数将告诉您放置在该单元格周围的12个值的标准偏差 如果我输入getstd(A1),它将得到A1,A3,A5,A7,C1,C3,C5,C7,E1,E3,E5和E7的标准偏差 如果我输入getstd(X23),它将获得12个其他值的std,这些值位于X23周围的相同偏移处 我现在最大的问题是弄清楚如何使用细胞作为输入 例如,在尝试偏移时: Function getstd(rng as Range)
Function getstd(rng as Range)
Range(rng).Offset(1,1) = "hello world"
End Function
它总是给我一个#值错误
我觉得如果我能让它工作,那么创建我的函数应该很容易
谢谢大家!
Michael您可以将(Union
)单元格分组到一个多区域范围内,并使用内置的stdev
功能:
Function getstd(ByVal target As Range)
Dim r As Range
For i = 0 To 6 Step 2
For j = 0 To 4 Step 2
If r Is Nothing Then Set r = target.Offset(i, j) Else Set r = Union(r, target.Offset(i, j))
Next
Next
getstd = Application.StDev(r)
End Function
您可以将(Union
)单元格分组到一个多区域范围内,并使用内置的stdev
功能:
Function getstd(ByVal target As Range)
Dim r As Range
For i = 0 To 6 Step 2
For j = 0 To 4 Step 2
If r Is Nothing Then Set r = target.Offset(i, j) Else Set r = Union(r, target.Offset(i, j))
Next
Next
getstd = Application.StDev(r)
End Function
另一种方法是:
Public Function STD_DEV() As Variant
Dim rDataSet As Range
Dim rCell As Range
Dim aValues(1 To 8) As Double
Dim x As Long
Application.Volatile
With Application.ThisCell
'Check we're not trying to reference off the sheet.
If .Row > 1 And .Column > 1 And _
.Row < Rows.Count And .Column < Columns.Count Then
'Get a reference to all cells around target cell.
'This includes the target cell in its reference.
Set rDataSet = .Offset(-1, -1).Resize(3, 3)
'Step through each cell in the range and add
'value to an array.
x = 1
For Each rCell In rDataSet
If rCell.Address <> .Address Then
aValues(x) = rCell.Value
x = x + 1
End If
Next rCell
'Calculate the Standard Deviation.
STD_DEV = WorksheetFunction.StDev(aValues)
Else
STD_DEV = CVErr(xlErrRef)
End If
End With
End Function
公共函数STD_DEV()作为变量
Dim rDataSet As范围
变暗rCell As范围
变暗aValues(1到8)为双精度
暗x等长
应用程序。挥发性
使用Application.ThisCell
“检查一下,我们没有试图在工作表外引用。
如果.Row>1和.Column>1和_
.Row
请注意WITH
关键字-介于WITH
和END WITH
之间,以开头的任何内容均指应用程序。此单元格
。所以.Row
与说Application.ThisCell.Row
编辑:已更新函数以返回#REF代码>错误,如果它试图从工作表外引用。另一种方法:
Public Function STD_DEV() As Variant
Dim rDataSet As Range
Dim rCell As Range
Dim aValues(1 To 8) As Double
Dim x As Long
Application.Volatile
With Application.ThisCell
'Check we're not trying to reference off the sheet.
If .Row > 1 And .Column > 1 And _
.Row < Rows.Count And .Column < Columns.Count Then
'Get a reference to all cells around target cell.
'This includes the target cell in its reference.
Set rDataSet = .Offset(-1, -1).Resize(3, 3)
'Step through each cell in the range and add
'value to an array.
x = 1
For Each rCell In rDataSet
If rCell.Address <> .Address Then
aValues(x) = rCell.Value
x = x + 1
End If
Next rCell
'Calculate the Standard Deviation.
STD_DEV = WorksheetFunction.StDev(aValues)
Else
STD_DEV = CVErr(xlErrRef)
End If
End With
End Function
公共函数STD_DEV()作为变量
Dim rDataSet As范围
变暗rCell As范围
变暗aValues(1到8)为双精度
暗x等长
应用程序。挥发性
使用Application.ThisCell
“检查一下,我们没有试图在工作表外引用。
如果.Row>1和.Column>1和_
.Row
请注意WITH
关键字-介于WITH
和END WITH
之间,以开头的任何内容均指应用程序。此单元格
。所以.Row
与说Application.ThisCell.Row
编辑:已更新函数以返回#REF代码>错误,如果它试图从工作表外引用。Range(rng).Offset(1,1
只需执行rng.Offset(1,1)
尽管@findwindow所说的语法是正确的,但UDF不会更改与调用方不同的单元格的值(没有一些古怪的侧身)。要了解Offset,请将行更改为getstd=rng.Offset(1,1).value
它将从指定的范围返回一行一列的值。请始终听Scott而不是我。@findwindow刚刚尝试过。仍然会给我一个#value错误。在单元格W23中,我键入=getstd(X12)
,我希望它显示“hello world”在单元格Y13中,但它不是…@ScottCraner谢谢,是的,这是有意义的。范围(rng).Offset(1,1
只需执行rng.Offset(1,1)
尽管@findwindow所说的语法是正确的,但UDF不会改变与调用方不同的单元格的值,(没有一些奇怪的侧身)。若要了解偏移量,请将行更改为getstd=rng.offset(1,1)。value
它将从指定范围返回一行一列的值。请始终听Scott的声音,而不要听我的声音。@findwindow刚刚尝试过。仍然会给我一个#value错误。在单元格W23中,我键入=getstd(X12)
,我希望它会显示“hello world”在Y13号牢房,但它没有…@ScottCraner谢谢,是的,这是有道理的。