2016-08-03 11 views
0

Ich bin ziemlich neu (gestern gestartet), also vielleicht hat jemand ein paar Vorschläge. Ich habe ein Makro erstellt, das ein Download-Sheet mit neuen Spalten und Formeln für Monatswerte aktualisiert, während die jährlichen und statischen Werte unverändert bleiben. Der Code führt seine beabsichtigte Funktion, sondern dauert fast 30 Minuten, um durch eine Schleife durch, wenn Lager = 25Verkürzung der Laufzeit des VBA-Makros

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

Da Sie beginnen, hier sind ein paar Tipps ... 1. 'CountIf' ist eine unzuverlässige Möglichkeit, die letzte Zeile zu finden. Vielleicht möchten Sie sehen [This] (http://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba/11169920#11169920) 2. Vermeiden Sie die Verwendung von '.Auswählen '. Möglicherweise möchten Sie [Dies] (http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) sehen. 3. Sie können Formeln in einem Bereich auf einmal eingeben, wenn sie zusammenhängend sind. Sie müssen das nicht individuell machen. –

+0

Für Bloomberg ist der beste Ansatz, die Funktion in Vba aufzurufen, indem Array übergeben wird, die Rückgabewerte in einem anderen Array gespeichert werden und das resultierende Array einfach auf dem Berichtsblatt/Datenblatt ausgegeben wird. Vermeiden Sie es, viele Formeln in Zellen zu schreiben. Speziell BDH ist asynchron, auf der anderen Seite wird es ziemlich unordentlich zu verwalten sein. – cyboashu

+0

Danke, das werde ich mir in Zukunft merken! –

Antwort

0

unter dem Sub Set New_monthly() gibt die folgende Zeile ein:

Application.ScreenUpdating = False 

und in der Beenden Sie die Funktion auf Application.ScreenUpdating = True.

In diesem Link finden Sie einige Ratschläge bekommen für Ihre Leistung zu verbessern:

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

+0

Toll, vielen Dank für Ihre Hilfe! Die Funktion wird jetzt in Sekunden ausgeführt! –

+0

@PeterisCelms sind herzlich willkommen! – ManInTheMiddle

1

Sie sollten ScreenUpdating und Berechnungen deaktivieren.

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, du hast vergessen, sie am Ende des Codes wieder einzuschalten, du schaffst sie zweimal atm;) – DragonSamu

+0

@DragonSamu Danke! –

+0

Und um sicher zu gehen, dass Sie sich nicht mit anderen Teilen des Codes herumärgern, sollten Sie die vorherigen Werte von ScreenUpdating und Calculation speichern und die gespeicherten Werte am Ende wiederherstellen (vielleicht war der Standardwert für Berechnung xlCalculationSemiautomatisch) –

1

Zusätzlich zu dem, was alle anderen gesagt hat, gibt es hier ein Artikel, der ich nützlich gefunden:

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

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

Im Wesentlichen diese am Anfang des Codes hinzufügen

und das am Ende:

With Application.Excel 
    .ScreenUpdating = True 
    .DisplayStatusBar = True 
    .Calculation = xlCalculationAutomatic 
    .EnableEvents = True 
End With 

Sie können auch

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

Persönlich obwohl ich, dass die größte Leistung im Allgemeinen mit Deaktivierung der Berechnungen kommt ein Upgrade gefunden haben, nutzen.