0
您好我正在使用一个函数,它将来自特定单元格的两个输入定义为范围。 当我运行代码时,该函数将取得范围的名称,但不包含该值的名称。 这些输入的范围位置是手动分配的,稍后将存储公式的结果。 下面的代码中,我突出显示了问题部分 关于如何将范围值应用到函数中的任何想法?如何将范围的值分配给函数vba
Sub my_range()
Dim RngWidth As Long, RngHeight As Long
Dim MyAverage As Variant, MyStDev As Variant, MyMax As Variant, MyMin As
Variant, MyCount As Variant
Dim StckReturns As Range
Dim MyAverage1 As Variant, MyStDev1 As Variant
Selection.End(xlToLeft).Select
Selection.End(xlUp).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
RngWidth = Selection.Columns.Count
RngHeight = Selection.Rows.Count
ActiveCell.Offset(0, RngWidth).Range("a1").Select
ActiveCell.FormulaR1C1 = "Daily Return"
ActiveCell.Offset(2, 0).Range("a1").Select
ActiveCell.FormulaR1C1 = "=StockReturn(R[-1]C[-1],RC[-1])"
Range(ActiveCell, ActiveCell).Select
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.Offset(RngHeight - 3, 0))
Set StckReturns = Range(ActiveCell, ActiveCell.Offset(RngHeight - 3, 0))
MyAverage = InputBox("Where do you want the Average?")
MyStDev = InputBox("Where do you want the Standard Deviation?")
Range(MyAverage).Value = WorksheetFunction.Average(StckReturns.Value)
Range(MyStDev).Value = WorksheetFunction.StDev(StckReturns.Value)
MyAverage1 = Range(MyAverage).Value
MyStDev1 = Range(MyStDev).Value
ActiveCell.Offset(-2, 1).Range("a1").Select
ActiveCell.FormulaR1C1 = "Scaled Return"
ActiveCell.Offset(2, 0).Range("a1").Select
***ActiveCell.FormulaR1C1 = "=ScaledReturns(RC[-1],myaverage1,MyStDev1)"***
Range(ActiveCell, ActiveCell).Select
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.Offset(RngHeight - 3, 0))
End Sub
Function StockReturn(IniPrice As Double, FinPrice As Double) As Double
StockReturn = (FinPrice - IniPrice)/IniPrice
End Function
Function ScaledReturns(MyReturn As Variant, AverageReturn As Variant, StDevReturn As Variant) As Double
ScaledReturns = (MyReturn - AverageReturn)/StDevReturn
End Function
太感谢你了!罗里!它现在工作完美 – Mariano