2016-08-03 3 views
0

これはかなり新しい(昨日から始まった)ので、多分誰か提案があります。私は毎年の値と静的な値を変更せずに、毎月の値の新しい列と数式でダウンロードシートを更新するマクロを作成しました。コードは、その意図された機能を実行するが、スルーループを通して循環に約30分かかる場合ストック= 25VBAマクロのランタイムを短くする

Sub New_monthly() 

    Dim i As Integer 

    Dim Measure As Integer 

    Dim Count As Integer 

    Dim Stock As Integer 

    Dim Monthend As Long 


    Stock = Application.WorksheetFunction.CountIf(Range("AP1:AP50"), ">0") 

    Monthend = Range("AS1").Value 


    For i = 0 To Stock - 1 
     ' Copy old values and move them over one space 

     Range("B54:B115").Offset(i * 115, 0).Select 
     Selection.Insert Shift:=xlToRight 

     ' Enter new values 

     Cells(108 + i * 115, 2).Value = Monthend 
     Cells(109 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(109 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(110 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(110 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(111 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(111 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(112 + i * 115, 2).Formula = "=BDH(" & Cells(112 + i * 115, 1).Address & "," & Cells(111 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(113 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(113 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(114 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(114 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(115 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(115 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 

     For Measure = 0 To 5 
      Cells(54 + i * 115 + Measure * 9, 3).Value = Monthend 
      For f = 0 To 7 
       Cells(55 + i * 115 + Measure * 9 + f, 3).Formula = "=BDH(" & Cells(19 + i * 115 + f, 1).Address & "," & Cells(54 + i * 115 + Measure * 9, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
      Next f 
      Cells(57 + i * 115 + Measure * 9, 3).Formula = "=AVERAGE(" & Cells(58 + i * 115 + Measure * 9, 3).Address & ":" & Cells(62 + i * 115 + Measure * 9, 3).Address & ")" 
     Next Measure 
    Next i 

End Sub 
+1

あなたが始めているので、ここにいくつかのヒントがあります... 1. 'CountIf'は最後の行を見つけるための信頼できない方法です。 [This](http://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba/11169920#11169920)を参照することができます。2. '.Select '。 [This](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)を見たいかもしれません。 3.連続している場合は、1つの範囲内の数式を入力できます。あなたはそれを個別に行う必要はありません。 –

+0

Bloombergにとって最良の方法は、配列を渡して戻り値を別の配列に格納し、結果の配列をレポートシート/データシートにダンプすることによって、Vbaで関数を呼び出すことです。多くの数式をセルに書くのは避けてください。特にBDHは非同期であり、管理するのが面倒になるだろう。 – cyboashu

+0

ありがとうございます、私は将来これを念頭に置いておきます! –

答えて

0

New_monthlyサブ以下に設定()は、次の行機能:

Application.ScreenUpdating = False 

とでをあなたの最後の機能はApplication.ScreenUpdating = Trueに設定してください。あなたのパフォーマンスを向上させるためにあなたはいくつかのアドバイスを取得します。このリンクで

http://datapigtechnologies.com/blog/index.php/ten-things-you-can-do-to-speed-up-your-excel-vba-code/

+0

おかげさまで、ありがとうございました!この関数は数秒で実行されます! –

+0

@PeterisCelmsようこそ。 – ManInTheMiddle

1

あなたはScreenUpdatingと計算をオフにする必要があります。誰もが言ったことに加えて

Sub New_monthly() 

    Dim i As Integer 
    Dim Measure As Integer 
    Dim Count As Integer 
    Dim Stock As Integer 
    Dim Monthend As Long 

    Stock = Application.WorksheetFunction.CountIf(Range("AP1:AP50"), ">0") 

    Monthend = Range("AS1").Value 

    With Application 
     .ScreenUpdating = False 
     .Calculation = xlCalculationManual 
    End With 

    For i = 0 To Stock - 1 
     ' Copy old values and move them over one space 

     Range("B54:B115").Offset(i * 115, 0).Select 
     Selection.Insert Shift:=xlToRight 

     ' Enter new values 

     Cells(108 + i * 115, 2).Value = Monthend 
     Cells(109 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(109 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(110 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(110 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(111 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(111 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(112 + i * 115, 2).Formula = "=BDH(" & Cells(112 + i * 115, 1).Address & "," & Cells(111 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(113 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(113 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(114 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(114 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
     Cells(115 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(115 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 

     For Measure = 0 To 5 

      Cells(54 + i * 115 + Measure * 9, 3).Value = Monthend 
      For f = 0 To 7 
       Cells(55 + i * 115 + Measure * 9 + f, 3).Formula = "=BDH(" & Cells(19 + i * 115 + f, 1).Address & "," & Cells(54 + i * 115 + Measure * 9, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")" 
      Next f 
      Cells(57 + i * 115 + Measure * 9, 3).Formula = "=AVERAGE(" & Cells(58 + i * 115 + Measure * 9, 3).Address & ":" & Cells(62 + i * 115 + Measure * 9, 3).Address & ")" 
     Next Measure 
    Next i 

    With Application 
     .ScreenUpdating = True 
     .Calculation = xlCalculationAutomatic 
    End With 

End Sub 
+0

psst、コードの最後に戻すのを忘れて、2回atmをオフにした;) – DragonSamu

+0

@DragonSamuありがとう! –

+0

そして、コードの他の部分を混乱させないように、ScreenUpdatingとCalculationの以前の値を保存し、最後に保存した値を復元する必要があります(何らかの理由でCalculationのデフォルト値がxlCalculationSemiautomatic) –

1

、私が役に立ったと評価してここに記事があります:

https://blogs.office.com/2009/03/12/excel-vba-performance-coding-best-practices/

は基本的にあなたのコードの先頭にこれを追加します。

With Application.Excel 
    .ScreenUpdating = False 
    .DisplayStatusBar = False 
    .Calculation = xlCalculationManual 
    .EnableEvents = False 
End With 

これが最後です:

また、私は計算を非アクティブ化が付属して最大のパフォーマンスは、一般的にアップグレードすることを発見したものの

ActiveSheet.DisplayPageBreaks = False 'at the beginning and True at the end 

個人的に使用することができます。