1

現在、株式投資のリターンを計算しています。私は約10年間の履歴データを持っていますが、関数を構築した方法では、仕事を実行するのに時間がかかります。たとえば、毎日の収益を計算するために、11 列と 2872 行があります。

my Function
     Public Sub CalcROI(ByVal ColPick As Integer, ByVal ColPrint As Integer)

     Dim irow As Integer
     Dim iCol As Integer

     For irow = 4 To 2873
    'Calculating ROI
      Cells(irow + 1, ColPrint).Value = (Cells(irow + 1, ColPick).Value - Cells(irow, ColPick).Value) / Cells(irow, ColPick).Value
      Next irow

      End Sub

そして、手順の実装は

 CalcROI ColPick:=4, ColPrint:=17

ColPick - 計算のために値を選択する必要がある場所から

ColPrint - 出力を印刷する列の必要性

4

2 に答える 2

1

これが機能するかどうかはわかりません。昨日別の質問で見たものをテストしたかっただけです。テストする場合は、ひどくうまくいかない場合に備えて、ワークブックのコピーで実行してください。

アップデート

私はそれをテストしました(乱数> 0の列を使用するだけです)、動作します。

Public Sub CalcROI(ByVal ColPick As Integer, ByVal ColPrint As Integer)
Dim rgPick As Range
Dim vaPick As Variant
Dim rgPrint As Range
Dim vaPrint As Variant
Dim Row As Integer

    Set rgPick = Range(Cells(4, ColPick), Cells(2873 + 1, ColPick))
    vaPick = rgPick.Value

    Set rgPrint = Range(Cells(4, ColPrint), Cells(2873 + 1, ColPrint))
    vaPrint = rgPrint.Value

    For Row = LBound(vaPick) To UBound(vaPick) - 1
        vaPrint(Row + 1, 1) = (vaPick(Row + 1, 1) - vaPick(Row, 1)) / vaPick(Row, 1)
    Next Row

    rgPrint = vaPrint

End Sub

私が参照した答え

于 2013-03-16T13:24:56.630 に答える
0

現在のコードを変更したくない場合は、利用Application.ScreenUpdatingすることでこれを行うことができます (また、将来のほぼすべての Excel VBA コードの実行を高速化することもできます)。


Public Sub CalcROI(ByVal ColPick As Integer, ByVal ColPrint As Integer)
     'this stops Excel from updating the screen after every single iteration in your loop
     'in the future, this is an EASY way to speed up the majority of Excel macros
     Application.screenupdating = false  
     Dim irow As Integer
     Dim iCol As Integer

     For irow = 4 To 2873
    'Calculating ROI
      Cells(irow + 1, ColPrint).Value = (Cells(irow + 1, ColPick).Value - Cells(irow, ColPick).Value) / Cells(irow, ColPick).Value
      Next irow
      'this isn't strictly speaking necessary, but will help
      application.screenupdating = true
      End Sub
于 2013-03-16T21:57:53.467 に答える